Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display date time in Java with Thai numerals

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.

like image 918
lukeg Avatar asked Sep 03 '17 20:09

lukeg


Video Answer


1 Answers

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));

like image 83
lukeg Avatar answered Sep 30 '22 13:09

lukeg