英文:
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: "Dec 9, 2016 4:36:00 PM"
答案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 = "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);
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 = "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'.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论