【问题标题】:Convert date object of java into the format of May 15th, 2018 [duplicate]将java的日期对象转换为2018年5月15日的格式[重复]
【发布时间】:2018-05-15 18:21:10
【问题描述】:

我想将java中的日期对象转换为上述格式的字符串。

请注意日期中应包含“st”或“nd”或“rd”或“th”

【问题讨论】:

标签: java date datetime-format


【解决方案1】:

这不是一个好的解决方案,但我通过以下方式得到了预期的结果:

public static void main(String[] args) {
        MainClass mainClass = new MainClass();
        Date date = new Date();
        System.out.println(mainClass.getFormattedDate(date));
    }

    public String getFormattedDate(Date date){
         SimpleDateFormat formatDayOfMonth  = new SimpleDateFormat("d");
         int day = Integer.parseInt(formatDayOfMonth.format(date));

         SimpleDateFormat dateStr = new SimpleDateFormat("d'st' 'of' MMMM yyyy");

         String dayStr = dateStr.format(date).replace("st", getSuffixes(day));
         return dayStr;
    }

    private String getSuffixes(int day) {
        switch(day%10) {
            case 1: return "st";
            case 2: return "nd";
            case 3: return "rd";
            default: return "th";
        }
    }

【讨论】:

    猜你喜欢
    • 2015-04-29
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多