英文:
Unable to parse date using LocalDateTime?
问题
我正在尝试使用DateTimeFormatter解析日期字符串。我收到了以下异常:
错误的日期格式 10.10.2020 12:00:00
java.time.format.DateTimeParseException: 无法解析文本'10.10.2020 12:00:00',位于索引2
String date = "10.10.2020 12:00:00";
String dateTimeFormat = "MM/dd/yyyy HH:mm:ss a";
String exportTimeZone = "UTC";
DateTimeFormatter format = DateTimeFormatter.ofPattern(dateTimeFormat);
LocalDateTime impDateTime = LocalDateTime.parse(StringUtils.trim(date), format);
ZonedDateTime dateInUtc = ZonedDateTime.ofInstant(impDateTime.atZone(ZoneId.of(exportTimeZone)).toInstant(), ZoneId.systemDefault());
return format.format(dateInUtc);
任何帮助将不胜感激。
英文:
I am trying to parse a date string using DateTimeFormatter. I am receiving the below exception:
Wrong date format 10.10.2020 12:00:00
java.time.format.DateTimeParseException: Text '10.10.2020 12:00:00' could not be parsed at index 2
String date="10.10.2020 12:00:00";
String dateTimeFormat = "MM/dd/yyyy HH:mm:ss a";
String exportTimeZone = "UTC";
DateTimeFormatter format = DateTimeFormatter.ofPattern(dateTimeFormat);
LocalDateTime impDateTime = LocalDateTime.parse(StringUtils.trim(date), format);
ZonedDateTime dateInUtc = ZonedDateTime.ofInstant(impDateTime.atZone(ZoneId.of(exportTimeZone)).toInstant(), ZoneId.systemDefault());
return format.format(dateInUtc);
Any help would be appreciated?
答案1
得分: 2
你在格式中使用了 /
而不是 .
。此外,当你使用 HH
时,表示使用的是24小时制,而不是 上午/下午
,因此在格式中不应该与 HH
一起使用 a
。另外,你的日期时间字符串中没有包含 上午/下午
,所以在格式中使用 a
无论如何都会导致错误。
示例:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateTime = "10.10.2020 12:00:00";
// 定义格式
DateTimeFormatter format = DateTimeFormatter.ofPattern("MM.dd.yyyy HH:mm:ss");
// 解析日期时间,使用定义的格式
LocalDateTime impDateTime = LocalDateTime.parse(dateTime, format);
// 获取 UTC 的日期时间
ZonedDateTime dateTimeInUtc = impDateTime.atZone(ZoneId.of("Etc/UTC"));
// 显示结果
System.out.println(dateTimeInUtc);
}
}
输出:
2020-10-10T12:00Z[Etc/UTC]
英文:
You have used /
instead of .
in the format. Also, when you are using HH
, it means it is 24-hour format, not am/pm
and therefore you shouldn't use a
with HH
in the format. Moreover, your date-time string does not have am/pm
in it, so using a
in the format will anyway cause an error.
Demo:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateTime = "10.10.2020 12:00:00";
// Define the format
DateTimeFormatter format = DateTimeFormatter.ofPattern("MM.dd.yyyy HH:mm:ss");
// Parse date-time as using the defined format
LocalDateTime impDateTime = LocalDateTime.parse(dateTime, format);
// Get date-time at UTC
ZonedDateTime dateTimeInUtc = impDateTime.atZone(ZoneId.of("Etc/UTC"));
// Display
System.out.println(dateTimeInUtc);
}
}
Output:
2020-10-10T12:00Z[Etc/UTC]
答案2
得分: 1
让我们阅读这条信息:
> java.time.format.DateTimeParseException:无法解析文本'10.10.2020 12:00:00',索引为2
索引是从0开始的,所以在10.10.2020 12:00:00
中,索引2是第一个点的位置。因此,Java无法解析该点。为了找出原因,让我们看看格式模式字符串中的相应位置
String dateTimeFormat = "MM/dd/yyyy HH:mm:ss a";
因此,月份10已成功解析,接下来格式化程序期望一个斜杠。斜杠和点之间的差异解释了异常。
进一步的提示:当为了正确解析而获得格式模式字符串变得非常复杂时,可以首先尝试格式化已知日期:
System.out.println("待解析:" + date);
LocalDateTime knownDateTime = LocalDateTime.of(2020, Month.OCTOBER, 10, 12, 0);
System.out.println("格式化后: " + knownDateTime.format(format));
在这种情况下的输出为:
> 待解析:10.10.2020 12:00:00
> 格式化后: 10/10/2020 12:00:00 PM
通过这种打印方式,更容易发现我们得到的内容与格式化程序解析所期望的内容之间的差异。
英文:
Let’s read the message:
> java.time.format.DateTimeParseException: Text '10.10.2020 12:00:00' could not be parsed at index 2
Indices are 0-based, so index 2 in 10.10.2020 12:00:00
is where the first dot (period, point) is. So Java is unable to parse that dot. To find out why, let’s look at the corresponding place in the format pattern string
String dateTimeFormat = "MM/dd/yyyy HH:mm:ss a";
So the month, 10, has been successfully parsed and next the formatter expects — a slash. The discrepancy between slash and dot explains the exception.
A further tip: When it gets non-trivial to get the format pattern string for parsing right, try formatting a known date first:
System.out.println("To be parsed: " + date);
LocalDateTime knownDateTime = LocalDateTime.of(2020, Month.OCTOBER, 10, 12, 0);
System.out.println("Formatted: " + knownDateTime.format(format));
Output in this case:
> To be parsed: 10.10.2020 12:00:00
> Formatted: 10/10/2020 12:00:00 PM
This way of printing it makes it easier to spot the differences between what we’ve got and what the formatter will expect for parsing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论