定义日期格式java'rd''st''th''nd'

定义日期格式java'rd''st''th''nd'

问题描述:

我有一个字符串 Saturday,Oct,25th,11:40

I have a String "Saturday, Oct, 25th, 11:40"

此日期的格式是什么?如何解析常规指标

What format does this date has? How can I parse the ordinal indicator?

这是我要如何转换

private String changeDateFormat(String stringDate){

        DateFormat dateFormat = new SimpleDateFormat("DD, MM, ddth, hh:mm");
        try {
            Date date = dateFormat.parse(stringDate);
            dateFormat = new SimpleDateFormat("ddMMMyyyy");
            stringDate=dateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return stringDate.toUpperCase();
    }


SimpleDateFormat 似乎不容易处理后跟 st, th等的天数。一种简单的解决方案是删除原始字符串的该部分。例如,

SimpleDateFormat doesn't seem to easily handle day numbers followed by "st" "th" etc. A simple solution would be remove that part of the original string. E.g.

int comma = original.lastIndexOf(',');
String stringDate =
        original.substring(0, comma - 2) +
        original.substring(comma + 1);

之后,只需在 stringDate 上使用此格式:

After that just use this format on stringDate:

SimpleDateFormat("EE, MM, dd, hh:mm")