I only have limited experience in Java libraries and in Android programming.
In Thailand both western (Arabic) digits and Thai digits are used. 2023 = ๒๐๒๓
Thailand also uses both the western calendar and the Thai Buddhist calendar. 2023 = 2566.
I know how to get a Thai locale Locale.forLanguageTag("th-TH") and how to get a version using Thai digits Locale.forLanguageTag("th-TH-u-nu-thai")
I know I can format a whole date in various Thai compatible ways using code like this:
val thaiNumberLocale = Locale.forLanguageTag("th-TH-u-nu-thai")
val thaiDecimalStyle = DecimalStyle.of(thaiNumberLocale)
val currentDateTime = LocalDateTime.now()
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
val date = formatter
.withLocale(thaiNumberLocale)
.withDecimalStyle(thaiDecimalStyle)
.withChronology(ThaiBuddhistChronology.INSTANCE)
.format(currentDateTime)
I know I can get just the day of the week or the month in Thai locale/Thai script with code like this:
val dayOfWeekInThai = currentDateTime.dayOfWeek.getDisplayName(TextStyle.FULL, thaiLocale)
val dayOfWeekInEnglish = currentDateTime.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH)
But now I don't want a whole formatted date string. I just want the year. But I want to be able to get it in Thai digits. This I can't figure out how to do using APIs related to those in the code shown.
I realize I can do text conversion a character at a time if I have to. But I'm assuming this functionality exists in the Java date/time and locale APIs and that I just can't find it.
Please, what am I missing?
Indeed I was missing something.
For
DateTimeFormatterthere were several built in formatters such as I showed in my code in my question:But you can also make a formatter from a pattern, and you can make a pattern with just a year like this:
So you can use a formatter with such a year-only pattern in conjunction with either or both
.withDecimalStyle()to get Thai digits and.withChronology(ThaiBuddhistChronology.INSTANCE)to get Thai Buddhist years.Here's the Jetpack Compose code I used to display all four combinations as an example: