Java的SimpleDateFormat,DateTimeFormatter:YYYY与yyyy

在Java的SimpleDateFormat类中格式化日期时,YYYY和yyyy之间存在细微的差异。它们都代表一年,但是yyyy代表日历年,而YYYY代表星期。这是一个细微的差异,仅会导致一年左右的变更问题,因此您的代码本可以一直正常运行,而仅在新的一年中引发问题。

一个例子比用文字更好地说明了这一点。

  1.  
    package com.dangoldin.test;
  2.  
     
  3.  
    import java.text.SimpleDateFormat;
  4.  
    import java.util.Date;
  5.  
     
  6.  
    public class Test {
  7.  
     
  8.  
    public static void main(String[] args) {
  9.  
    try {
  10.  
    String[] dates = {"2018-12-01", "2018-12-31", "2019-01-01"};
  11.  
    for (String date: dates) {
  12.  
    SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
  13.  
    Date d = dt.parse(date);
  14.  
     
  15.  
    SimpleDateFormat dtYYYY = new SimpleDateFormat("YYYY");
  16.  
    SimpleDateFormat dtyyyy = new SimpleDateFormat("yyyy");
  17.  
     
  18.  
    System.out.println("For date " + date + " the YYYY year is " + dtYYYY.format(d) + " while for yyyy it's " + dtyyyy.format(d));
  19.  
    }
  20.  
    } catch (Exception e) {
  21.  
    System.out.println("Failed with exception: " + e);
  22.  
    }
  23.  
    }
  24.  
    }

这为您提供以下内容:

  1.  
    For date 2018-12-01 the YYYY year is 2018 while for yyyy it's 2018
  2.  
    For date 2018-12-31 the YYYY year is 2019 while for yyyy it's 2018
  3.  
    For date 2019-01-01 the YYYY year is 2019 while for yyyy it's 2019

自两年格式匹配以来,第一个和最后一个有意义。中间一个是奇数之一。日期开始于2018-12-31,但是YYYY给您2019,而yyyy给您2018。通常,您几乎应该始终使用yyyy,因此,添加某种形式的lint或检查以确保您的代码没有引用YYYY的任何日期格式。

  1.  
    import java.text.SimpleDateFormat;
  2.  
    import java.time.LocalDateTime;
  3.  
    import java.time.format.DateTimeFormatter;
  4.  
    import java.util.Date;
  5.  
     
  6.  
    public class Example {
  7.  
    public static void main(String[] args) {
  8.  
    LocalDateTime currentDateTime = LocalDateTime.now();
  9.  
     
  10.  
    DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy");
  11.  
    String formatDateTime = currentDateTime.format(format1);
  12.  
    System.out.println(formatDateTime);
  13.  
     
  14.  
    DateTimeFormatter format2 = DateTimeFormatter.ofPattern("YYYY");
  15.  
    String formatDateTime2 = currentDateTime.format(format2);
  16.  
    System.out.println(formatDateTime2);
  17.  
     
  18.  
    Date date = new Date();
  19.  
     
  20.  
    SimpleDateFormat simpleformat1 = new SimpleDateFormat("yyyy");
  21.  
    String formatDateTime3 = simpleformat1.format(date);
  22.  
    System.out.println(formatDateTime3);
  23.  
     
  24.  
    SimpleDateFormat simpleformat2 = new SimpleDateFormat("YYYY");
  25.  
    String formatDateTime4 = simpleformat2.format(date);
  26.  
    System.out.println(formatDateTime4);
  27.  
     
  28.  
    }
  29.  
    }

12月31日执行结果:

2020
2021
2020
2021

转载:https://blog.csdn.net/allway2/article/details/112058361?utm_medium=distribute.pc_category.none-task-blog-hot-15.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-15.nonecase

原文地址:https://www.cnblogs.com/coder-ahao/p/14223977.html