0

I'm using Java 8 and I have a string in my .txt file which I want to convert into a LocalDateTime object.

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);

This gives me this exception:

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)

Any ideas?

P.S. Note that this:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy. HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

doesn't work in Java 8.

Edit: I didn't make the question clear enough, my bad:

I have this string in my .txt file, and I need to convert it to LocalDateTime object in order to save it into a class object, but I need it in the format stated in order to print it out inside a table. I don't want it to print out in the raw format that is "2017-10-06T17:48:23.558". I want it to print out something like this: "10.06.2017. 17:48:23"

mdenci
  • 297
  • 2
  • 6
  • 17

5 Answers5

6

The format you want for output ("dd.MM.yyyy. HH:mm:ss") is not the same as the input, so you can't use it to parse.

In this specific case, the input is in ISO8601 format, so you can just parse it directly. Then you use the formatter to format the LocalDateTime object to the format you want:

String time1 = "2017-10-06T17:48:23.558";
// convert String to LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(time1);
// parse it to a specified format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss");
System.out.println(localDateTime.format(formatter));

The output is:

06.10.2017. 17:48:23


PS: If the input was in a different format, you should use one formatter to parse and another one to format. Check the javadoc to see all available formats.

  • yes, this works, thank you, and I'm sorry for not making my first post clear enough... will this line `System.out.println(localDateTime.format(formatter));` work in javafx? – mdenci Oct 17 '17 at 09:50
  • @mdenci Actually it's better to set the result of format method to a `String` and then you do anything you need with it –  Oct 17 '17 at 09:53
1

Use LocalDateTime.parse without any additional format to parse it into a LocalDateTime:

jshell> java.time.LocalDateTime ldt = java.time.LocalDateTime.parse("2017-10-06T17:48:23.558");
ldt ==> 2017-10-06T17:48:23.558
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
  • 1
    This works because the string is in the standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, which is what `LocalDateTime.parse(...)` understands. – Jesper Oct 17 '17 at 09:30
1

Your date formatter pattern is wrong. You need to give the same format as of that string you are passing

Example:

String date = "2016-08-16T10:15:30+08:00";

    ZonedDateTime result = ZonedDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);

    System.out.println("ZonedDateTime : " + result);

    System.out.println("TimeZone : " + result.getZone());

    LocalDate localDate = result.toLocalDate();

    System.out.println("LocalDate : " + localDate);
jarvo69
  • 7,908
  • 2
  • 18
  • 28
  • okay, the edited post works but the one you posted earlier really didn't work. but still, this is not what I'm looking for, but thanks anyway, maybe I'll need it later on – mdenci Oct 17 '17 at 09:32
1

You just got the pattern wrong. It would work if you used a pattern like this:

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

The patterns are well documented in the javadoc: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

0
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
a.l.
  • 1,085
  • 12
  • 29