开发者社区> 问答> 正文

倒序输出年月

要求当前系统的年份月份,然后进行倒序输出.该怎么做呢

展开
收起
蛮大人123 2016-02-21 16:35:26 1999 0
2 条回答
写回答
取消 提交回答
  • SimpleDateFormat直接搞定了吧

    2019-07-17 18:46:07
    赞同 展开评论 打赏
  • 我说我不帅他们就打我,还说我虚伪
    public class test4 {
        private Calendar c =null;
        public test4(int year,int month,int date){
            c = new GregorianCalendar(); 
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH,month);
            c.set(Calendar.DATE, date);
        }
        public test4(int year,int month){
            c = new GregorianCalendar(); 
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DATE, 1);
        }
        public String getStrByDate(Date date){
            String fmt = "";
            fmt = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            String dateStr = sdf.format(date);
            return dateStr;
        }
        public String getStrByMonth(Date date){
            String fmt = "";
            fmt = "yyyy-MM";
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            String dateStr = sdf.format(date);
            return dateStr;
        }
        public String getNextDay(int days) {
            c.add(Calendar.DATE,days*(-1));
            return this.getStrByDate(c.getTime());
        }
        public String getNextMonth(int months) {
            c.add(Calendar.MONTH,months*(-1));
            return this.getStrByMonth(c.getTime());
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            test4 cdtest = new test4(2015,2);
            for(int i=0;i<12;i++){
                System.out.println(cdtest.getNextMonth(1));
            }
        }
    
    }
    2019-07-17 18:46:07
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载