定义日期格式java'rd''st''th''nd' [英] define the date format 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")
这篇关于定义日期格式java'rd''st''th''nd'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文