英文:
Convert String to LocalDateTime Java 8
问题
我正在尝试将以下字符串转换为LocalDateTime:
String dateStr = "2020-08-17T10:11:16.908732";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnn");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, format);
但是我遇到了以下错误:
java.time.format.DateTimeParseException: 无法解析文本 '2020-08-17T10:11:16.908732',位于索引 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
请问是否有人可以帮忙提供建议,我应该如何正确地将字符串格式化为LocalDateTime?
非常感谢
英文:
I'm trying to convert the following String into a LocalDateTime:
String dateStr = "2020-08-17T10:11:16.908732";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnn");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, format);
But I'm hitting the following error:
java.time.format.DateTimeParseException: Text '2020-08-17T10:11:16.908732' could not be parsed at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Can anyone please help to advise how I should be correctly formatting the string into a LocalDateTime?
Many thanks
答案1
得分: 4
你在这种情况下不需要指定DateTimeFormatter
,因为如果你不传递任何参数,将会使用默认的格式:
public static void main(String[] args) {
String dateStr = "2020-08-17T10:11:16.908732";
// 以下代码隐式地使用了DateTimeFormatter.ISO_LOCAL_DATE_TIME
LocalDateTime dateTime = LocalDateTime.parse(dateStr);
System.out.println(dateTime);
}
这段代码将输出 2020-08-17T10:11:16.908732
。
如果你坚持要使用自定义的DateTimeFormatter
,需要将模式中的T
用单引号括起来,并且不要使用n
来解析纳秒部分(S
代表毫秒),否则结果可能会错误。
按照下面的方式编写:
public static void main(String[] args) {
String dateStr = "2020-08-17T10:11:16.908732";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, format);
System.out.println(dateTime);
}
输出与上述代码相同。
注意:
使用模式"yyyy-MM-dd'T'HH:mm:ss.nnnnnn"
得到的结果与解析的String
不会相等,而会是:
2020-08-17T10:11:16.000908732
英文:
You don't need to specify a DateTimeFormatter
in this case because the default one will be used if you don't pass one at all:
public static void main(String[] args) {
String dateStr = "2020-08-17T10:11:16.908732";
// the following uses the DateTimeFormatter.ISO_LOCAL_DATE_TIME implicitly
LocalDateTime dateTime = LocalDateTime.parse(dateStr);
System.out.println(dateTime);
}
That code will output 2020-08-17T10:11:16.908732
.
If you are insisting on using a custom DateTimeFormatter
, consider the T
by single-quoting it in the pattern and don't use nanosecond parsing (n
) for parsing fractions of second (S
), the result might be wrong otherwise.
Do it like this:
public static void main(String[] args) {
String dateStr = "2020-08-17T10:11:16.908732";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, format);
System.out.println(dateTime);
}
with the same output as above.
Note:
The result of using the pattern "yyyy-MM-dd'T'HH:mm:ss.nnnnnn"
would not be equal to the parsed String
, instead, it would be
2020-08-17T10:11:16.000908732
答案2
得分: 1
要更新给定的 DateTime 字符串模式,应该使用 "yyyy-MM-dd'T'HH:mm:ss.nnnnnn"
。
所以代码应该像这样:
String dateStr = "2020-08-17T10:11:16.908732";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, format);
关于此问题的更多详细信息,请参考 JavaDoc。
另外,在您提供的输入 DateTime 中,它使用了 6 位数字,所以它不能是纳秒,因为纳秒是 1/1000000000。所以正确的格式应该是秒的小数部分,有 6 位数字,如 "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
。
最终结果比较:
使用模式 : "yyyy-MM-dd'T'HH:mm:ss.nnnnnn"
System.out.println(LocalDateTime.parse("2020-08-17T10:11:16.908732", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn")));
输出 : 2020-08-7T10:11:16.000908732
使用模式 : "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
System.out.println(LocalDateTime.parse("2020-08-17T10:11:16.908732", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")));
输出 : 2020-08-7T10:11:16.908732
英文:
For your given DateTime string pattern should be updated "yyyy-MM-dd'T'HH:mm:ss.nnnnnn"
.
So the code should be like :
String dateStr = "2020-08-17T10:11:16.908732";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, format);
For more details around it you can refer JavaDoc.
Along that in your given input DateTime it's using 6 digits, so it can't be nano seconds. Because nano is 1/1000000000. So it will have at least 9 digits. So the correct format rather should be second fraction with 6 digits as "yyyy-MM-dd'T'HH:mm:ss.SSSSSS".
End Results comparison:
With Pattern : "yyyy-MM-dd'T'HH:mm:ss.nnnnnn"
System.out.println(LocalDateTime.parse("2020-08-17T10:11:16.908732", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn")));
> Output : 2020-08-7T10:11:16.000908732
With Pattern : "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
System.out.println(LocalDateTime.parse("2020-08-17T10:11:16.908732", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")));
> Output : 2020-08-7T10:11:16.908732
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论