我有两个日期,需要格式化他们,但我收到以下异常.我的主要问题是th rd etc当天的部分.我找不到这个问题的答案.我检查了所有这些链接1,2,3,4,5,我想我应该用正则表达式,但不知道如何.
10th Dec 2019 -> 2019-12-10
10th December 2019 -> 2019-12-10
Run Code Online (Sandbox Code Playgroud)
码
String date1 = "10th Dec 2019";
Date date = new SimpleDateFormat("dd MMMM YYYY").parse(date1);
System.err.println(date);
String date2 = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.err.println(date2);
Run Code Online (Sandbox Code Playgroud)
例外
Exception in thread "main" java.text.ParseException: Unparseable date: "10th Dec 2019"
Run Code Online (Sandbox Code Playgroud)
用空字符串替换所有预期的不需要的sufiix,然后解析:
String s = "10th Dec 2019";
SimpleDateFormat fmt = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
Date d = fmt.parse(s.replaceFirst("th|nd|st|rd", ""));
System.out.println("d = " + d);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
486 次 |
| 最近记录: |