我知道如何在Java中使用SimpleDateFormat。但解析所有日期后最好的方法是什么?喜欢这些:" 2014年5月25日"," 2014年5月3日"," 2014年5月1日"。你看到字母可能有什么不同?所以我不想为每个数字结尾创建单独的格式化程序。在Java中使用它是更好的方法吗?
答案 0 :(得分:2)
您可以使用replaceAll
将所有这些转换为常用的,然后将其放入单个格式化程序字符串中。
theDate = theDate.replaceAll("(?:(st|nd|rd|th))","xx");
if (theDate.contains("guxx")) // Handle fixing "August" becoming "Auguxx"
theDate = theDate.replace("guxx","gust");
这会将1st 2nd 3rd 4th 5th
更改为1xx 2xx 3xx 4xx 5xx
。现在你可以使用静态字符串' xx'在你的模式中。