Displaying AM and PM in lower case after date formatting

后端 未结 8 1113
谎友^
谎友^ 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:04

    Unfortunately the standard formatting methods don't let you do that. Nor does Joda. I think you're going to have to process your formatted date by a simple post-format replace.

    String str = oldstr.replace("AM", "am").replace("PM","pm");
    

    You could use the replaceAll() method that uses regepxs, but I think the above is perhaps sufficient. I'm not doing a blanket toLowerCase() since that could screw up formatting if you change the format string in the future to contain (say) month names or similar.

    EDIT: James Jithin's solution looks a lot better, and the proper way to do this (as noted in the comments)

提交回复
热议问题