mdenci
mdenci

Reputation: 307

Converting String into LocalDateTime using Java 8 DateTimeFormatter

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"

Upvotes: 0

Views: 14997

Answers (5)

user7605325
user7605325

Reputation:

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.

Upvotes: 6

a.l.
a.l.

Reputation: 1135

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

Upvotes: 0

jarvo69
jarvo69

Reputation: 8339

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

Upvotes: 1

user366222
user366222

Reputation:

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

Upvotes: 1

M. le Rutte
M. le Rutte

Reputation: 3563

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

Upvotes: 1

Related Questions