在日期格式化后以小写字母显示AM和PM [英] displaying AM and PM in small letter after date formatting
问题描述
格式化日期后,我显示时间显示上午或下午(大写字母)
i想要小写像am,pm
这是我的代码
After formatting date i am displaying time which is showing AM or PM(in Upper case) i want it in lower case like am,pm this is my code
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");
您可以使用 replaceAll()
方法使用regepxs,但我认为上面的内容可能就足够了。我不做毯子 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屋!