Parsing string to local date doesn't use desired century

后端 未结 1 1961
失恋的感觉
失恋的感觉 2020-11-27 07:52

I am using this DateTimeFormatter:

DateTimeFormatter.ofPattern(\"ddMMYY\")

I want to parse the str

相关标签:
1条回答
  • 2020-11-27 08:39

    Since this question is really about new java.time-package and NOT SimpleDateFormat I will cite following relevant section:

    Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.

    We see that Java-8 uses the range 2000-2099 per default, not like SimpleDateFormat the range -80 years until +20 years relative to today.

    If you want to configure it then you have to use appendValueReduced(). This is designed in an inconvenient way, but possible, see here:

    String s = "150790";
    
    // old code with base range 2000-2099
    DateTimeFormatter dtf1 = 
      new DateTimeFormatterBuilder().appendPattern("ddMMyy").toFormatter();
    System.out.println(dtf1.parse(s)); // 2090-07-15
    
    // improved code with base range 1935-2034
    DateTimeFormatter dtf2 = 
      new DateTimeFormatterBuilder().appendPattern("ddMM")
      .appendValueReduced(
        ChronoField.YEAR, 2, 2, Year.now().getValue() - 80
      ).toFormatter();
    System.out.println(dtf2.parse(s)); // 1990-07-15
    

    By the way, if you really want week-based years then you have to use Y instead of y or the appropriate field IsoFields.WEEK_BASED_YEAR. Regarding the fact that you don't have any other week-related fields I would assume the normal calendar year, not the week-based one.

    0 讨论(0)
提交回复
热议问题