“May 28, 2013 6:04:38 PM” 是哪种格式?

huangapple go评论72阅读模式
英文:

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));

huangapple
  • 本文由 发表于 2020年4月7日 02:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61066939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定