日期格式化后以小写形式显示AM和PM [英] Displaying AM and PM in lower case after date formatting
问题描述
格式化日期时间后,时间以大写形式显示AM或PM,但我希望以小写形式显示,例如am或pm.
After formatting a datetime, the time displays AM or PM in upper case, but I want it in lower case like am or pm.
这是我的代码:
public class Timeis {
public static void main(String s[]) {
long ts = 1022895271767L;
String st = null;
st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(ts);
System.out.println("time is " + ts);
}
}
推荐答案
不幸的是,标准格式设置方法不允许您这样做.乔达也没有.我认为您将必须通过简单的后格式替换来处理格式日期.
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");
您可以使用使用regepxs的replaceAll()
方法,但是我认为以上内容就足够了.我不做一个全面的toLowerCase()
,因为如果将来更改格式字符串以包含(例如)月份名称或类似名称,这可能会破坏格式.
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.
James Jithin的解决方案看起来要好得多,并且执行此操作的正确方法(如评论中所述) )
James Jithin's solution looks a lot better, and the proper way to do this (as noted in the comments)
这篇关于日期格式化后以小写形式显示AM和PM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!