important : 应写成 yyyy-MM-dd
1.java 时间
2019-12-31 转 YYYY/MM/dd 格式: 2020/12/31
2020-01-01 转 YYYY/MM/dd 格式: 2020/01/01
2019-12-31 转 yyyy/MM/dd 格式: 2019/12/31
2020-01-01 转 yyyy/MM/dd 格式: 2020/01/01
2.yyyy 和YYYY
@Test
public void testWeekBasedYear() {
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
SimpleDateFormat formatYYYY = new SimpleDateFormat("YYYY/MM/dd");
System.out.println("2019-12-31 转 YYYY/MM/dd 格式: " + formatYYYY.format(strDate1));
System.out.println("2020-01-01 转 YYYY/MM/dd 格式: " + formatYYYY.format(strDate2));
// 小写 YYYY
SimpleDateFormat formatyyyy = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("2019-12-31 转 yyyy/MM/dd 格式: " + formatyyyy.format(strDate1));
System.out.println("2020-01-01 转 yyyy/MM/dd 格式: " + formatyyyy.format(strDate2));
}
3.DD 与dd
12/29/2019 将会格式化到2019年 这一周还属于2019年
12/30/2019 将会格式化到2020年 这一周已经属于2020年
=2020 M=01 D=20 formatted with "MM/DD/YYYY" -> 01/20/2020
Y=2020 M=01 D=21 formatted with "DD/MM/YYYY" -> 21/01/2020
Y=2020 M=01 D=22 formatted with "YYYY-MM-DD" -> 2020-01-22
Y=2020 M=03 D=17 formatted with "MM/DD/YYYY" -> 03/77/2020
Y=2020 M=03 D=18 formatted with "DD/MM/YYYY" -> 78/03/2020
Y=2020 M=03 D=19 formatted with "YYYY-MM-DD" -> 2020-03-79
DD代表的是处于这一年中那一天,不是处于这个月的那一天。
网友评论