我正在使用Java 8,并且在我的.txt
文件中有一个字符串,我想将其转换为LocalDateTime
对象。使用Java 8将字符串转换为LocalDateTime DateTimeFormatter
String time1 = "2017-10-06T17:48:23.558";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss");
LocalDateTime alarmTime = LocalDateTime.parse(time1, formatter1);
System.out.println(time1);
这给了我这样的例外:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-10-06T17:48:23.558' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
任何想法?
P.S.需要注意的是这样的:
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy. HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);
没有在Java中工作8
编辑:我没有这个问题不够清楚,我的坏:
我有这个字符串我.txt
文件,并且我需要将它转换为LocalDateTime
对象以便将其保存到类对象中,但我需要它以所述的格式将其打印出来以便在表格中打印出来。我不希望它以原始格式打印出"2017-10-06T17:48:23.558"
。我希望它打印出来是这样的:"10.06.2017. 17:48:23"
你做了一个完全错误的模式... – Phoenix
在你的日期索引'2'没有'。'按照您的模式暗示 – A4L
'DateTimeFormat.forPattern'来自Joda-Time,而'DateTimeFormatter.ofPattern'来自Java 8'java.time' API。你想使用哪一个? – 2017-10-17 09:33:31