3

The java.time framework built into Java 8 and later parses two-digit year strings as being in the 2000s. So 90 becomes 2090.

From the java.time.DateTimeFormatter class documentation:

If the count of letters is two… will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.

I have data where values such as 90 meant 1990.

How can I change the pivot year for parsing two-digit year strings to 1900 rather than 2000?

Even better, how can I set any arbitrary pivot year, as I know some data sources have rules for partial century. For example, “If under 80, assume 2000s, otherwise assume 1900s”.

Yes, I know, using two-digit year values is dopey. But the data was not under my control.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    I think I would parse as normal, then if the year was > 2079, subtract 100 from the year. – markspace Feb 07 '16 at 21:10
  • 1
    What about this answer? http://stackoverflow.com/a/29496149/1743880 (maybe this one also: http://stackoverflow.com/a/32783553/1743880). – Tunaki Feb 07 '16 at 21:10
  • 1
    I never understood why people were lazy to save the other 2 digits for the year, but since the Y2K problem 16 years passed!!! How come there's still living code like that? – Gavriel Feb 07 '16 at 21:11

1 Answers1

0

Just use the API to get a value in the 2000-2099 range and use some conditionals to do the rest of the work. Don't worry about trying to change the API itself.

TW80000
  • 1,507
  • 1
  • 11
  • 18
  • Actually, java.time does support overriding this assumption of 2000s. You can easily set your own pivot year. See [this Answer](http://stackoverflow.com/a/32505814/642706). – Basil Bourque Feb 07 '16 at 21:31