英文:
Which format of "May 28, 2013 6:04:38 PM"?
问题
我尝试使用SimpleDateFormat将字符串“May 28, 2013 6:04:38 PM”解析为日期,但我无法成功。以下是我所做的:
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a");
try {
System.out.println(sdf.parse("May 28, 2013 6:04:38 PM"));
} catch (ParseException e) {
e.printStackTrace();
}
}
错误信息:
java.text.ParseException: 无法解析的日期: "May 28, 2013 6:04:38 PM"
at java.text.DateFormat.parse(DateFormat.java:377)
at com.test(DateUtil.java:71)
英文:
I trying parse the String "May 28, 2013 6:04:38 PM" to Date with SimpleDateFormat, but I couldn't do it. Below is what I did:
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a");
try {
System.out.println(sdf.parse("May 28, 2013 6:04:38 PM"));
} catch (ParseException e) {
e.printStackTrace();
}
}
the error:
java.text.ParseException: Unparseable date: "May 28, 2013 6:04:38 PM"
at java.text.DateFormat.parse(DateFormat.java:377)
at com.test(DateUtil.java:71)
答案1
得分: 2
尝试这个:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM d, y h:mm:ss a");
String datestr = "May 28, 2013 1:04:38 PM";
LocalDateTime dateTime = LocalDateTime.parse(datestr, dtf);
System.out.println(dateTime.format(dtf));
英文:
Try this:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM d, y h:mm:ss a");
String datestr = "May 28, 2013 1:04:38 PM";
LocalDateTime dateTime = LocalDateTime.parse(datestr, dtf);
System.out.println(dateTime.format(dtf));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论