Displaying AM and PM in lower case after date formatting

后端 未结 8 1107
谎友^
谎友^ 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<Long, String> 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.

    0 讨论(0)
  • 2020-11-27 07:14
    Calendar c = Calendar.getInstance();
    
    System.out.println("Current time => " + c.getTime());
    
    SimpleDateFormat df = new SimpleDateFormat("HH:mm a");
    String formattedDate = df.format(c.getTime());
    formattedDate = formattedDate.replace("a.m.", "AM").replace("p.m.","PM");
    
    TextView textView = findViewById(R.id.textView);
    textView.setText(formattedDate);
    
    0 讨论(0)
提交回复
热议问题