来自 https://www.v2ex.com/t/633650
YYYY 是表示:当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,那么这周就算入下一年。
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.
u year
y year-of-era
Y week-based-year
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)); }