日期格式后以小写显示 AM 和 PM [英] Displaying AM and PM in lower case after date formatting

查看:30
本文介绍了日期格式后以小写显示 AM 和 PM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

格式化日期时间后,时间以大写显示 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");

您可以使用使用 regpxs 的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆