[Java]SimpleDateFormat日期时间格式化,显示上午/下午,非显示AM/PM

2021-10-15 2505点热度 0人点赞 1条评论

希望格式化当前的时间为:2021年10月14日 下午 1时30分

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh时mm分");
System.out.println(sdf.format(new Date()));
// 打印出来可能是 2021年10月14日 PM 01时30分
// 也可能是 2021年10月14日 下午 01时30分

会根据操作系统的语言等环境变化,尤其是部署到Linux服务器上后,上午和下午,可能会显示AM/PM

为了保证中文输出,我们需要为SimpleDateFormat特别指定Locale,例如:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh时mm分", Locale.CHINESE);
System.out.println(sdf.format(new Date()));

// 打印出一定是上午/下午了 2021年10月14日 下午 01时30分

 

admin

这个人很懒,什么都没留下

文章评论

  • Ranker SEO

    Hello,

    Greetings from Ranker SEO.

    Are you looking for a genuine SEO service to rank your website top on Google? Are you cheated by fake SEO companies?

    You have found an affordable legitimate SEO agency to rank your website.

    We have ranked 100s of websites such as blogs, shopify stores, ecommerce websites, adult websites, saas websites, etc. You can reach more customers by utilizing our backlinks.

    For real proof, you can visit our website and check out. Contact us for more details.

    Our Skype ID: [email protected]
    Our Website: https://www.ranker.one

    Thanks & Regards
    Ranker SEO

    If you don't like to receive our messages, you can unsubscribe by click here https://www.ranker.one/unsubscribe/?site=terrynow.com

    2022-05-13
  • 您需要 登录 之后才可以评论