我需要用Java解析格式"2010年1月10日"的日期.我怎样才能做到这一点?
如何处理好序的指标中,st,nd,rd,或th尾随天数?
这有效:
String s = "January 10th, 2010";
DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
System.out.println("" + dateFormat.parse(s.replaceAll("(?:st|nd|rd|th),", "")));
Run Code Online (Sandbox Code Playgroud)
但您需要确保使用Locale正确解析月份名称的权限.
我知道你可以在SimpleDateFormat模式中包含一般文本.但是在这种情况下,文本依赖于信息,实际上与解析过程无关.
这实际上是我能想到的最简单的解决方案.但我希望被证明是错的.
您可以通过执行与此类似的操作来避免在其中一条评论中暴露的陷阱:
String s = "January 10th, 2010";
DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
System.out.println("" + dateFormat.parse(s.replaceAll("(?<= \\d+)(?:st|nd|rd|th),(?= \\d+$)", "")));
Run Code Online (Sandbox Code Playgroud)
例如,这将允许您不匹配Jath,uary 10 2010.
| 归档时间: |
|
| 查看次数: |
4958 次 |
| 最近记录: |