Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeFormatter.forPattern inconsistency with US Locale and SimpleDateFormatter when parsing weeks #792

Open
victornoel opened this issue Sep 27, 2024 · 0 comments

Comments

@victornoel
Copy link

victornoel commented Sep 27, 2024

Key information

  • Joda-Time version: 2.13.0
  • Result of TimeZone.getDefault(): sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
  • Result of DateTimeZone.getDefault(): UTC

Problem description

SimpleDateFormatter is dependent on the Locale and in particular for the pattern yyyy-'W'ww, it will return different weeks numbers:

jshell> new java.text.SimpleDateFormat("yyyy-'W'ww", Locale.US).format(new Date(2022-1900, 12-1, 26))
$11 ==> "2022-W53"

jshell> new java.text.SimpleDateFormat("yyyy-'W'ww", Locale.UK).format(new Date(2022-1900, 12-1, 26))
$12 ==> "2022-W52"

On the other hand, DateTimeFormatter.forPattern will refuse, even when using Locale.US, to parse week 53 and anyway will return an incorrect date for the US locale and one of the week before.

Test case

The following works for Locale.UK but not Locale.US:

var date = new Date(2022-1900, 12-1, 26);
var locale = Locale.US;
var pattern = "yyyy-'W'ww";
assertEquals(
    new Date(
        new DateTimeFormatter.forPattern(pattern).withLocale(locale).parseMillis(
            new SimpleDateFormat(pattern, locale).format(date)
        )
    ),
    date
);
@victornoel victornoel changed the title DateTimeFormatter.forPattern inconsistecy with US Locale and SimpleDateFormatter when parsing weeks DateTimeFormatter.forPattern inconsistency with US Locale and SimpleDateFormatter when parsing weeks Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant