I want to format a date, which should be localized and includes the "day period".
"Day period" meaning not am/pm but "afternoon", "early morning" etc.
A formatted date based on german locale with the 12 hour setting enabled should look like 03.08.18, 9:01 abends
, "abends" meaning evening.
The same example should be 3/8/18 9:01 de la noche
in spanish.
Why i think it should be possible:
A: Because the WhatsApp Android app formats dates like this
B: Because the Java 10 source code includes the following (From the german translation file):
<dayPeriods>
<dayPeriodContext type="format">
<dayPeriodWidth type="wide">
<dayPeriod type="afternoon">nachmittags</dayPeriod>
<dayPeriod type="am">vorm.</dayPeriod>
<dayPeriod type="earlyMorning">morgens</dayPeriod>
<dayPeriod type="evening">abends</dayPeriod>
<dayPeriod type="morning">vormittags</dayPeriod>
<dayPeriod type="night">nachts</dayPeriod>
<dayPeriod type="noon">Mittag</dayPeriod>
<dayPeriod type="pm">nachm.</dayPeriod>
</dayPeriodWidth>
</dayPeriodContext>
...
</dayPeriods>
I tried: (current is the locale)
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, current);
String formattedDate = df.format(System.currentTimeMillis());
Output: 08.12.18 7:24 nachm
or 8/12/18 7:54 p. m.
The following code has the same output:
DateFormat abc = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, current);
abc.format(System.currentTimeMillis());
I also tried the
DateTimeFormatterBuilder
(With all of the AMPM_OF_DAY versions):
DateTimeFormatter test = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.append(test)
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.SHORT)
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.NARROW)
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL)
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL_STANDALONE)
.toFormatter(current);
ZonedDateTime date = ZonedDateTime.now();
date.format(fmt);
Output: 8 dic. 2018 19:54p. m.p. m.p. m.11
Is there any way i can output the date this way?
Thanks