在 Java 中将字符串转换为日期时遇到问题
Trouble converting String to Date in Java
我在 String
中有一个日期:
String string = "16.03.2017, 09:22";
我正在尝试将其转换为 Date
。
Locale russianLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build();
Date date = new SimpleDateFormat("DD.MM.YYYY, HH:mm",russianLocale).parse(string);
无论我给这个函数赋什么值,它都会打印日期 "Mon Dec 26 09:22:00 MSK 2016"。时间值是当前值,但日期始终相同。
这是怎么造成的,我该如何解决?
您的格式设置不正确。使用的代码区分大小写。 dd
和 yyyy
应该是小写的。
你还忽略了时区这个关键问题。
并且您正在使用麻烦的旧式日期时间 classes,例如 Date
和 SimpleDateFormat
。请改用现代 java.time classes。 Stack Overflow 上关于此主题的数百个现有问题和答案。搜索 class 个名称 ZoneId、LocalDateTime、ZonedDateTime 和 DateTimeFormatter。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu, HH:mm" );
LocalDateTime ldt = LocalDateTime.parse( "16.03.2017, 09:22" , f );
您的日期格式不正确。使用此行替换代码中的行:
date = new SimpleDateFormat("dd.MM.yyyy, HH:mm", russianLocale).parse(string);
完整代码:
private static Date convertStringToDate(String string) {
Date date = new Date();
Locale russianLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build();
try {
date = new SimpleDateFormat("dd.MM.yyyy, HH:mm", russianLocale).parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
希望对您有所帮助!
只需更改 SimpleDateFormat cunstructor 中的格式。您使用了错误的格式化程序 DD.MM.YYYY、HH:mm。所以只需将其替换为 "dd.MM.yyyy, HH:mm"
private static Date convertStringToDate(String string) {
日期 date = new Date();
Locale russianLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build();
尝试 {
date = new SimpleDateFormat("dd.MM.yyyy, HH:mm",russianLocale).parse(字符串);
} 赶上(ParseException e){
e.printStackTrace();
}
return 日期;
}
我在 String
中有一个日期:
String string = "16.03.2017, 09:22";
我正在尝试将其转换为 Date
。
Locale russianLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build();
Date date = new SimpleDateFormat("DD.MM.YYYY, HH:mm",russianLocale).parse(string);
无论我给这个函数赋什么值,它都会打印日期 "Mon Dec 26 09:22:00 MSK 2016"。时间值是当前值,但日期始终相同。
这是怎么造成的,我该如何解决?
您的格式设置不正确。使用的代码区分大小写。 dd
和 yyyy
应该是小写的。
你还忽略了时区这个关键问题。
并且您正在使用麻烦的旧式日期时间 classes,例如 Date
和 SimpleDateFormat
。请改用现代 java.time classes。 Stack Overflow 上关于此主题的数百个现有问题和答案。搜索 class 个名称 ZoneId、LocalDateTime、ZonedDateTime 和 DateTimeFormatter。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu, HH:mm" );
LocalDateTime ldt = LocalDateTime.parse( "16.03.2017, 09:22" , f );
您的日期格式不正确。使用此行替换代码中的行:
date = new SimpleDateFormat("dd.MM.yyyy, HH:mm", russianLocale).parse(string);
完整代码:
private static Date convertStringToDate(String string) {
Date date = new Date();
Locale russianLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build();
try {
date = new SimpleDateFormat("dd.MM.yyyy, HH:mm", russianLocale).parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
希望对您有所帮助!
只需更改 SimpleDateFormat cunstructor 中的格式。您使用了错误的格式化程序 DD.MM.YYYY、HH:mm。所以只需将其替换为 "dd.MM.yyyy, HH:mm"
private static Date convertStringToDate(String string) { 日期 date = new Date(); Locale russianLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build(); 尝试 { date = new SimpleDateFormat("dd.MM.yyyy, HH:mm",russianLocale).parse(字符串); } 赶上(ParseException e){ e.printStackTrace(); } return 日期; }