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

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

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

请检查时间格式:

  1. String dateString1 = "Dec 9, 2016 4:36:00 PM";
  2. String dateString2 = "Jan 12, 2017 11:36:15 AM";
  3. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy h:mm:ss a");
  4. LocalDateTime localDateTime1 = LocalDateTime.parse(dateString1, formatter);
  5. LocalDateTime localDateTime2 = LocalDateTime.parse(dateString2, formatter);
  6. System.out.println(localDateTime1);
  7. 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:

  1. String dateString1 = &quot;Dec 9, 2016 4:36:00 PM&quot;;
  2. String dateString2 = &quot;Jan 12, 2017 11:36:15 AM&quot;;
  3. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;MMM d, yyyy h:mm:ss a&quot;);
  4. LocalDateTime localDateTime1 = LocalDateTime.parse(dateString1, formatter);
  5. LocalDateTime localDateTime2 = LocalDateTime.parse(dateString2, formatter);
  6. System.out.println(localDateTime1);
  7. 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.

  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import org.junit.Test;
  4. public class blam
  5. {
  6. private static final String VALUE1 = "Dec 9, 2016 4:36:00 PM";
  7. private static final String VALUE2 = "Jan 12, 2017 11:36:15 AM";
  8. @Test
  9. public void test()
  10. {
  11. DateTimeFormatter dateTimeFormatter;
  12. LocalDateTime value;
  13. dateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, u h:m:s a");
  14. value = LocalDateTime.parse(VALUE1, dateTimeFormatter);
  15. System.out.println("value1. " + VALUE1 + " becomes: " + value);
  16. value = LocalDateTime.parse(VALUE2, dateTimeFormatter);
  17. System.out.println("value2. " + VALUE2 + " becomes: " + value);
  18. }
  19. }

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.

  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import org.junit.Test;
  4. public class blam
  5. {
  6. private static final String VALUE1 = &quot;Dec 9, 2016 4:36:00 PM&quot;;
  7. private static final String VALUE2 = &quot;Jan 12, 2017 11:36:15 AM&quot;;
  8. @Test
  9. public void test()
  10. {
  11. DateTimeFormatter dateTimeFormatter;
  12. LocalDateTime value;
  13. dateTimeFormatter = DateTimeFormatter.ofPattern(&quot;MMM d, u h:m:s a&quot;);
  14. value = LocalDateTime.parse(VALUE1, dateTimeFormatter);
  15. System.out.println(&quot;value1. &quot; + VALUE1 + &quot; becomes: &quot; + value);
  16. value = LocalDateTime.parse(VALUE2, dateTimeFormatter);
  17. System.out.println(&quot;value2. &quot; + VALUE2 + &quot; becomes: &quot; + value);
  18. }
  19. }

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:

确定