0

My code is

ZonedDateTime zoned = ZonedDateTime.now();
DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.forLanguageTag("ja-JP"));
System.out.println(zoned.format(pattern));

And the output is 2022/06/13, which is fine. But when I change the locale to en-US, the date is Jun 13, 2022. I want to have all locale-based date to only have numbers and slashes, like 2022/06/13, 06/13/2022. How can I do that?

2
  • 5
    I guess the stupid question is why? Formatting a date/time for the user should respect the users locale formatting, because not everybody represents the date in the same (ie 3/4/5 has different meaning to different people in different locations). If you're formatting the date for "internal" representation (ie for API calls etc), then you should really be using a ISO standard format - just saying Commented Jun 13, 2022 at 4:21
  • I suggest that your users will be happier with what you already have. If you insist, you can get a format pattern from DateTimeFormatterBuilder.getLocalizedDateTimePattern(), identify the order of y, M and d and manually replace yy with either y or yyyy. Commented Jun 13, 2022 at 14:06

1 Answer 1

1

you can try:

DateTimeFormatter pattern = DateTimeFormatter
                .ofPattern("yyyy/MM/dd")
                .withLocale(Locale.forLanguageTag("ja-JP")); // or "en-US"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.