[Java] December 28, 2020 will be formatted as December 28, 2021?

Trigger

When I was working on a screen with a WEB system that I was involved in on December 28, 2020, another member said, "Is there a screen that says" December 28, 2021 "one year ahead?" That was said

To conclude first, in the date format "yyyy/mm/dd"not,**"yyyy/mm/dd"**The cause was that it was described in.

verification code

verification code


import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) throws Exception {
    final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
    final SimpleDateFormat YYYYMMdd = new SimpleDateFormat("YYYY-MM-dd");
    
    Date date = yyyyMMdd.parse("2020-12-28");
    
    System.out.println(yyyyMMdd.format(date));
    //=> "2020-12-28"
    System.out.println(YYYYMMdd.format(date));
    //=> "2021-12-28"
  }
}

Commentary

SimpleDateFormat - JavaDoc y: year Y: Base year of calendar week The difference between y and Y is described above when reading JavaDoc.

To put it simply, the "base year of the calendar week" means that the day of the same week as January 1st is the year of January 1st. If the day of the week starts on Sunday, the year is treated as follows, for example. image.png

In addition, the following are related elements in the base year of the calendar week.

Start of the day Calendar.html#getFirstDayOfWeek Minimum number of days required for the first week of the year Calendar.html#getMinimalDaysInFirstWeek

If you do not explicitly specify firstDayOfWeek and minimalDaysInFirstWeek, the default values ​​are used according to the default locale. If Japan_Japanese is the default locale ("ja_JP"), it will be as follows.

Difference between default locale and value for each locale


import java.util.Calendar;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    Locale defaultLocale = Locale.getDefault();
    System.out.println(defaultLocale);
    //=> ja_JP
    
    // "ja_JP"Japan
    {
      Locale locale = new Locale("ja","JP");
      Calendar calendar = Calendar.getInstance(locale);
      // 1: SUNDAY, 2: MONDAY, 3: TUESDAY, 4: WEDNESDAY, 5: THURSDAY, 6: FRIDAY
      System.out.println(calendar.getFirstDayOfWeek());
      // => 1 (SUNDAY)
      System.out.println(calendar.getMinimalDaysInFirstWeek());
      // => 1 (1 day)
    }
    
    // "en_US"America
    {
      Locale locale = new Locale("en","US");
      Calendar calendar = Calendar.getInstance(locale);
      System.out.println(calendar.getFirstDayOfWeek());
      // => 1 (SUNDAY)
      System.out.println(calendar.getMinimalDaysInFirstWeek());
      // => 1 (1 day)
    }
    
    // "fr_FR"France
    {
      Locale locale = new Locale("fr","FR");
      Calendar calendar = Calendar.getInstance(locale);
      System.out.println(calendar.getFirstDayOfWeek());
      // => 2 (MONDAY)
      System.out.println(calendar.getMinimalDaysInFirstWeek());
      // => 4 (4 days ISO 8601 standard)
    }
  }
}

Unless explicitly specified as below, it seems that the week starting on Sunday and including January 1st is treated as belonging to the same year as January 1st.

Explicit specification example


Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY); //Start on Monday
calendar.setMinimalDaysInFirstWeek(4);       //ISO 8601 standard

Afterword

Thanks to the same development members who gave me the opportunity to write these articles!

reference

The first site I referred to Be careful about the difference between YYYY and yyyy in SimpleDateFormat-Fat DEV Diary

Why are the values ​​different only in France? Reference site when checking Week starts on Monday: French calendar (11) French day of the week (etymology, etc.)-Living French memoirs

Recommended Posts

[Java] December 28, 2020 will be formatted as December 28, 2021?
[Java beginner] Resource leak <~~> will never be closed
Java formatted output [Notes]
Save Java HTML as PDF
Java Stream cannot be reused.
JavaScript as seen from Java
It seems that data class-like functions will be added in Java14