我正在使用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