Displaying AM and PM in lower case after date formatting

后端 未结 8 1114
谎友^
谎友^ 2020-11-27 06:40

After formatting a datetime, the time displays AM or PM in upper case, but I want it in lower case like am or pm.

This is my code:

public class Timei         


        
8条回答
  •  庸人自扰
    2020-11-27 07:11

    If you don't want to do string substitution, and are using Java 8 javax.time:

    Map ampm = new HashMap<>();
    ampm.put(0l, "am");
    ampm.put(1l, "pm");
    
    DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .appendPattern("E M/d h:mm")
            .appendText(ChronoField.AMPM_OF_DAY, ampm)
            .toFormatter()
            .withZone(ZoneId.of("America/Los_Angeles"));
    

    It's necessary to manually build a DateTimeFormatter (specifying individual pieces), as there is no pattern symbol for lowercase am/pm. You can use appendPattern before and after.

    I believe there is no way to substitute the default am/pm symbols, making this is the only way short of doing the string replace on the final string.

提交回复
热议问题