什么是这种情况的正确日期格式?

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

What's the correct date format for this case?

问题

我一直在尝试将字符串解析为日期,但是转换不起作用。

例如,我有这些字符串,从一个网络服务中检索到。

Dec 9, 2016 4:36:00 PM
Jan 12, 2017 11:36:15 AM

我尝试使用这些格式,但是转换失败了。

MMM d, yyyy HH:mm:ss aaa
MMM d, yyyy hh:mm:ss a

我错在哪里?

我收到了这个异常
java.text.ParseException: Unparseable date: "Dec 9, 2016 4:36:00 PM"

英文:

I've been trying to parse String into Date, But, The conversion isn't working.

For example, I've these Strings, retrieved from a webservice.

Dec 9, 2016 4:36:00 PM<br>
Jan 12, 2017 11:36:15 AM

I've tried to use these formats, But, The conversion failed.

MMM d, yyyy HH:mm:ss aaa<br>
MMM d, yyyy hh:mm:ss a

Where am I wrong?

I'm getting this exception <br>
java.text.ParseException: Unparseable date: &quot;Dec 9, 2016 4:36:00 PM&quot;

答案1

得分: 2

请检查时间格式:

String dateString1 = "Dec 9, 2016 4:36:00 PM";
String dateString2 = "Jan 12, 2017 11:36:15 AM";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy h:mm:ss a");

LocalDateTime localDateTime1 = LocalDateTime.parse(dateString1, formatter);
LocalDateTime localDateTime2 = LocalDateTime.parse(dateString2, formatter);

System.out.println(localDateTime1);
System.out.println(localDateTime2);

在这种情况下,时间部分的格式是:h:mm:ss a(因为小时是4,而不是04),所以格式 hh:mm:ss a 会失败。

对于您的输入,打印输出将是:
2016-12-09T16:36
2017-01-12T11:36:15

英文:

Please check the time format:

String dateString1 = &quot;Dec 9, 2016 4:36:00 PM&quot;;
String dateString2 = &quot;Jan 12, 2017 11:36:15 AM&quot;;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;MMM d, yyyy h:mm:ss a&quot;);

LocalDateTime localDateTime1 = LocalDateTime.parse(dateString1, formatter);
LocalDateTime localDateTime2 = LocalDateTime.parse(dateString2, formatter);

System.out.println(localDateTime1);
System.out.println(localDateTime2);

See the format in this case (time part) is: h:mm:ss a (because the hour is 4, not 04), so the format hh:mm:ss a will fail

For your inputs, the print will be:
2016-12-09T16:36
2017-01-12T11:36:15

答案2

得分: 2

This is just a variation of the @C.P.O answer and uses a slightly different pattern.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.Test;

public class blam
{
	private static final String VALUE1 = "Dec 9, 2016 4:36:00 PM";
	private static final String VALUE2 = "Jan 12, 2017 11:36:15 AM";

	@Test
	public void test()
	{
		DateTimeFormatter dateTimeFormatter;
		LocalDateTime value;

		dateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, u h:m:s a");

		value = LocalDateTime.parse(VALUE1, dateTimeFormatter);
		System.out.println("value1. " + VALUE1 + " becomes: " + value);


		value = LocalDateTime.parse(VALUE2, dateTimeFormatter);
		System.out.println("value2. " + VALUE2 + " becomes: " + value);
	}
}

Some notes:

  • y is "year of era"
  • u is "year"
  • MMM is required. Neither "M" nor "MM" will work in your case.
  • I prefer "s" to "ss" because "s" will parse both 2 and 12, but "ss" will fail to parse "2"

I don't know the real impact of using 'y' instead of 'u'.

英文:

This is just a variation of the @C.P.O answer and uses a slightly different pattern.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.Test;

public class blam
{
	private static final String VALUE1 = &quot;Dec 9, 2016 4:36:00 PM&quot;;
	private static final String VALUE2 = &quot;Jan 12, 2017 11:36:15 AM&quot;;

	@Test
	public void test()
	{
		DateTimeFormatter dateTimeFormatter;
		LocalDateTime value;

		dateTimeFormatter = DateTimeFormatter.ofPattern(&quot;MMM d, u h:m:s a&quot;);

		value = LocalDateTime.parse(VALUE1, dateTimeFormatter);
		System.out.println(&quot;value1. &quot; + VALUE1 + &quot; becomes: &quot; + value);


		value = LocalDateTime.parse(VALUE2, dateTimeFormatter);
		System.out.println(&quot;value2. &quot; + VALUE2 + &quot; becomes: &quot; + value);
	}
}

Some notes:

  • y is "year of era"
  • u is "year"
  • MMM is required. Neither "M" nor "MM" will work in your case.
  • I prefer "s" to "ss" because "s" will parse both 2 and 12, but "ss" will fail to parse "2"

I don't know the real impact of using 'y' instead of 'u'.

huangapple
  • 本文由 发表于 2020年8月13日 22:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63396925.html
匿名

发表评论

匿名网友

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

确定