Date display time in Java with Thai digits

This exercise comes from the Horstmann Core Java book for the impatient:

Write a program that demonstrates date and time formatting styles in [...] Thailand (with Thai numbers).

I tried to solve the exercises 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 although the month name is given correctly in Thai (at least I think so, because I don’t know Thai), the numbers are still formatted with Arabic numbers, 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 for the program to work as I want? I am using JDK 1.8.0_131 for Windows 10 64 bit.

+5
source share
1 answer

DateTimeFormatter::withDecimalStyle

I was able to solve the exercise. For the formatter, pass DecimalStyle by calling DateTimeFormatter::withDecimalStyle like this (see 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));
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)); 
+4
source

Source: https://habr.com/ru/post/1271480/


All Articles