Java时间格式化遇到的坑

Java时间格式化时YYYY(大写)和yyyy(小写)的有区别,其中y 是Year、Y 表示的是Week year;Week year意思是当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,那么这周就算入下一年。2017-12-31是周日,刚好是2017最后一周并跨年,就取到2018年了;

1 public static void main(String[] args) throws ParseException {
2         SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
3         Date currentDate=df.parse("2017-12-31");
4         System.out.println("yyyy-MM-dd:"+DateFormatUtils.format(currentDate,"yyyy-MM-dd"));
5         System.out.println("YYYY-MM-dd:"+DateFormatUtils.format(currentDate,"YYYY-MM-dd"));
6     }

测试结果:
yyyy-MM-dd:2017-12-31
YYYY-MM-dd:2018-12-31

 

posted on 2019-12-19 16:08  成长与蜕变  阅读(948)  评论(0编辑  收藏  举报

导航