來自 https://www.v2ex.com/t/633650spa
YYYY 是表示:當天所在的周屬於的年份,一週從週日開始,週六結束,只要本週跨年,那麼這周就算入下一年。code
If a week is split at the end of the year then it is assigned to the year in which more that half of the days of that week occur.orm
u year
y year-of-era
Y week-based-yearblog
public static void main(String[] args) { DateFormat df = new SimpleDateFormat("yyyyMMdd"); System.out.println(df.format(new Date())); DateFormat df1 = new SimpleDateFormat("YYYYMMdd"); System.out.println(df1.format(new Date())); Calendar calendar = Calendar.getInstance(); // 2019-12-31 calendar.set(2019, Calendar.DECEMBER, 31); Date strDate1 = calendar.getTime(); // 2020-01-01 calendar.set(2020, Calendar.JANUARY, 1); Date strDate2 = calendar.getTime(); // 大寫 YYYY DateFormat formatUpperCase = new SimpleDateFormat("YYYY/MM/dd"); System.out.println("2019-12-31 to YYYY/MM/dd: " \+ formatUpperCase.format(strDate1)); System.out.println("2020-01-01 to YYYY/MM/dd: " \+ formatUpperCase.format(strDate2)); // 小寫 YYYY DateFormat formatLowerCase = new SimpleDateFormat("yyyy/MM/dd"); System.out.println("2019-12-31 to yyyy/MM/dd: " \+ formatLowerCase.format(strDate1)); System.out.println("2020-01-01 to yyyy/MM/dd: " \+ formatLowerCase.format(strDate2)); }