Get locale specific date/time format in Java
Asked Answered
T

3

6

I have use case in java where we want get the locale specific date. I am using DateFormat.getDateInstance

final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,
                Locale.forLanguageTag(locale)));

This translates the dates but ja-JP this translates the date "17 January 2019" to "2019/01/17" but I need something like "2019年1月17日". For all other locales this correctly translates the date.

Please let know if there is other method to get this.

Thence answered 17/1, 2019 at 7:31 Comment(2)
try just Locale.JAPAN kodejava.org/how-do-i-format-date-using-a-locale-based-formatLungki
The DateFormat class is notoriously troublesome and fortunately long outdated. Instead use DateTimeFormatter and LocalDate, both from java.time, the modern Java date and time API.Verily
L
9

This worked for me:

public static void main(String[] args) throws IOException {
    final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.JAPAN);
    Date today = new Date();
    System.out.printf("%s%n", dateFormat.format(today));
}

and MEDIUM acted exactly how you said

UPD: or using newer ZonedDataTime as Michael Gantman suggested:

public static void main(String[] args) throws IOException {
    ZonedDateTime zoned = ZonedDateTime.now();
    DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.JAPAN);
    System.out.println(zoned.format(pattern));
}
Lungki answered 17/1, 2019 at 7:40 Comment(6)
FormatStyle.FULL also returns the date with the time "2019年1月17日 9時57分43秒 UTC". I do not want the current time.Thence
To get only the date use ofLocalizedDate instead of ofLocalizedDateTime. Then you may also (optionally) use LocalDate instead of ZonedDateTime.Verily
Sorry, there should be used DateTimeFormatter.ofLocalizedDate not DataTime. FixedLungki
Works fine now. Thanks a lotThence
Ok this is wired now, some locals have Day as well. Ex: for "fr-FR" output is 'jeudi 17 janvier 2019'Thence
I don't speak neither Japanese nor French but maybe you should use custom format then: DateTimeFormatter.ofPattern("yyyy MMMM dd").withLocale(Locale.FRANCE); gives 2019 janvier 17 - maybe in France its common to give day name as wellLungki
L
3

Just to mention: SimpleDateFormat is an old way to format dates which BTW is not thread safe. Since Java 8 there are new packages called java.time and java.time.format and you should use those to work with dates. For your purposes you should use class ZonedDateTime Do something like this:

ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("..."));

to find out correct zone id for Japan use

ZoneId.getAvailableZoneIds()

Later on to format your Date correctly use class DateTimeFormatter

Lycian answered 17/1, 2019 at 8:2 Comment(1)
This provides not only a very direct but also a very good answer to the question “Please let know if there is other method to get this.”Verily
C
2

The trick is to use java.time.format.FormatStyle.LONG:

jshell> java.time.format.DateTimeFormatter.ofLocalizedDate(java.time.format.FormatStyle.LONG).withLocale(java.util.Locale.JAPAN)
$13 ==> Localized(LONG,)

jshell> java.time.LocalDate.now().format($13)
$14 ==> "2019年1月17日"
Cropdusting answered 17/1, 2019 at 7:47 Comment(1)
This is the solution that works on my Java 9. With Locale.forLanguageTag("ja-JP") too.Verily

© 2022 - 2024 — McMap. All rights reserved.