A way to format uppercase DEC and lowercase pm
I’m just giving a small supplement to @Deadpool’s good answer. A way of getting the month abbreviation in all caps like DEC
and am/pm in lowercase is a DateTimeFormatterBuilder
and its two-arg appendText(TemporalField, Map<Long,String>)
method.
Locale userLocale = Locale.US;
Map<Long, String> monthNames = new HashMap<>();
for (Month m : Month.values()) {
monthNames.put(Long.valueOf(m.getValue()),
m.getDisplayName(TextStyle.SHORT, userLocale).toUpperCase(userLocale));
}
Map<Long, String> amPmNames = new HashMap<>(3);
amPmNames.put(0L, "am");
amPmNames.put(1L, "pm");
DateTimeFormatter outputFormatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.MONTH_OF_YEAR, monthNames)
.appendPattern(" d | E | hh:mm ")
.appendText(ChronoField.AMPM_OF_DAY, amPmNames)
.toFormatter(userLocale);
OffsetDateTime odt = OffsetDateTime.parse("2019-12-25T17:00:00-05:00");
String formattedDate = odt.format(outputFormatter);
System.out.println(formattedDate);
Output:
DEC 25 | Wed | 05:00 pm
I have assumed that you are programming for Android under API level 26 and using ThreeTenABP, so I have tested on my desktop Java 7 with ThreeTen Backport. From Java 8 and on newer API levels and even more from Java 9 more elegant ways to populate the two maps exist.
What went wrong in your code?
The lowercase z
that comes last in your format pattern string matches a time zone name. The documentation gives examples Pacific Standard Time and PST. It doesn’t match an offset like -05:00
. If you ever need a format pattern letter for this, use x
, X
or uppercase Z
(they are different and all well documented). However, as already shown in Deadpool’s answer, you need no explicit formatter and no format pattern string in this case.
Another issue in your code: The LocalDate
class that you used in the question is a date without time of day. You can parse your string into a LocalDate
, but the time of day and offset get lost. Hence you cannot format the LocalDate
into a string that contains time of day. For that you need a class that has DateTime
in its name, for example OffsetDateTime
used in the two answers.
Link
DateTimeFormatter
documentation spelling out all the possible format pattern letters.