先上代码

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));

结果:

2019-12-31 to YYYY/MM/dd: 2020/12/31
2020-01-01 to YYYY/MM/dd: 2020/01/01
2019-12-31 to yyyy/MM/dd: 2019/12/31
2020-01-01 to yyyy/MM/dd: 2020/01/01

知乎链接:https://zhuanlan.zhihu.com/p/100648038

应该会有人踩坑。。。快去锤他


↙↙↙阅读原文可查看相关链接,并与作者交流