美文网首页
2020-06-09 java 操作时间格式 YYYY-M

2020-06-09 java 操作时间格式 YYYY-M

作者: 早起早起早起up | 来源:发表于2020-06-09 11:41 被阅读0次

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代表的是处于这一年中那一天,不是处于这个月的那一天。

相关文章

网友评论

      本文标题:2020-06-09 java 操作时间格式 YYYY-M

      本文链接:https://www.haomeiwen.com/subject/fbrstktx.html