首页 > 解决方案 > 添加日期格式并以时间格式提供 am/pm

问题描述

日期 API 中是否有任何特定格式可以在特定日期之后打印 st、nd、rd 和 th。说 12 月 26 日应该是 12 月 26 日。

还有任何特定的时间格式,比如上午 10.30 格式应该完全相同。

只需要内置模式来解决这个问题。

我已经为场景编写了下面的代码,但没有得到任何想要的输出

    //Checking date format
    String pattern = "EEE, MMM.dd";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    String date = simpleDateFormat.format(new Date());
    System.out.println(date);

    //checking time format

    DateFormat dateFormat2 = new SimpleDateFormat("hh.mm aa.");
    String dateString2 = dateFormat2.format(new Date()).toString();
    System.out.println(dateString2);

标签: java

解决方案


不,没有这种格式。正如您在文档中看到的那样,日期和时间模式SimpleDateFormat不提供诸如26th.


推荐阅读