This exercise comes from Horstmann's book Core Java for the impatient:
Write a program that demonstrates the date and time formatting styles in [...] Thailand (with Thai digits).
I tried to solve the exerciese with the following snippet:
Locale locale = Locale.forLanguageTag("th-TH-TH");
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(formatter.withLocale(locale).format(dateTime));
The problem is that while the name of month is given properly in Thai (at least I think so, since I don't know Thai), the numbers are still formatted with Arabic numerals, the output is as follows:
3 ก.ย. 2017, 22:42:16
I tried different language tags ("th-TH"
, "th-TH-TH"
, "th-TH-u-nu-thai"
) to no avail. What should I change to make the program behave as desired? I use JDK 1.8.0_131 on Windows 10 64 bit.
DateTimeFormatter::withDecimalStyle
I was able to solve the exercise. One must pass a DecimalStyle
to the formatter by calling DateTimeFormatter::withDecimalStyle
, like this (see the code in bold for changes):
Locale locale = Locale.forLanguageTag("th-TH-u-nu-thai");
LocalDateTime dateTime = LocalDateTime.now();
DecimalStyle style = DecimalStyle.of(locale);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(formatter.withLocale(locale).withDecimalStyle(style).format(dateTime));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With