有下面一段代碼:
public static void main(String[] args) { DateTime date = new DateTime(2019, 12, 31, 0, 0); SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd"); System.out.println(format.format(date.toDate())); SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(format2.format(date.toDate())); }
控制台輸出的結果是,不信你可以試下:
2020-12-31
2019-12-31
為什么會這樣呢,那是因為YYYY是week-based-year,表示當天所在的周屬於的年份,一同從周日開始,周六結束,只要本周跨年那么這周就算入下一個年份中。所以2019年12月31日那天在這種轉換方式下就是2020年。而當倩yyyy這種情況下年份就是2019。