Java时间格式化遇到的坑

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

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

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

转载:https://www.cnblogs.com/water-xu/p/12068401.html

猜你喜欢

转载自www.cnblogs.com/haoyuekey/p/12118390.html