-2

I'm trying to convert a String with a value of a LocalDateTime and convert it to the LocalDateTime data type.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");

LocalDateTime localDateTime = LocalDateTime.parse(dateTime, formatter);

and I've also tried using this:

LocalDateTime localDateTime = LocalDateTime.parse(dateTime);

But gets the same parsing error

java.time.format.DateTimeParseException: Text '2019-09-12T11:14:38.176906Z' could not be parsed at index 10

Value of the dateTime variable is (Generated before using LocalDateTime.now())

2019-09-12T11:14:38.176906Z
mengmeng
  • 1,266
  • 2
  • 22
  • 46
  • Using a pattern that __matches__ your date String would be helpful here. – Tom Sep 12 '19 at 11:33
  • isn't there a `T` between date and time in the String that is not matched in the pattern (expects empty space)? `2019-09-12T11` cannot be matched by `yyyy-MM-dd HH` (at index 10 as reported in Exception) – user85421 Sep 12 '19 at 11:35
  • @Tom it is generated from `LocalDateTime.now()` directly. I'm not quite sure what is the pattern on that. – mengmeng Sep 12 '19 at 11:39
  • 1
    Your string represents an `Instant` (or `OffsetDateTime`), not a `LocalDateTime`. The `Z` at the end stands for the UTC offset (`+00:00`). `LocalDateTime` will also never produce a value with a `Z` at the end. – TiiJ7 Sep 12 '19 at 11:40
  • Your String date is generated by `LocalDateTime.now()`, yes, but your pattern isn't and the most obvious different is the letter `T`. You've ignored that in your pattern. – Tom Sep 12 '19 at 11:43
  • 1
    @Tom I've traced the code. I apologize. Someone generated teh LocalDateTime but when passing to my function, the changed it to `ISO_INSTANT` string. Please check the edited code above. – mengmeng Sep 12 '19 at 11:46
  • No you misunderstood me. You String `2019-09-12T11:14:38.176906Z` is fine. Your pattern `yyyy-MM-dd HH:mm:ss.SSSX` is wrong. So it is not really important how you generated your String, because that part is fine and working correctly. – Tom Sep 12 '19 at 11:49

1 Answers1

0

You can use one of the standard DateTimeFormatters:
DateTimeFormatter.ISO_DATE_TIME or DateTimeFormatter.ISO_OFFSET_DATE_TIME
(your String appears to be an offset from UTC).

For the output, you can then use which one you want:

String datetime = "2019-09-12T11:14:38.176906Z";
OffsetDateTime ldt = OffsetDateTime.parse(datetime, DateTimeFormatter.ISO_DATE_TIME);
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

Output in this example is: 2019-09-12 11:14:38.

Tom
  • 16,842
  • 17
  • 45
  • 54
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • `ldt`.format() generates a `String` and not a `LocalDateTime` data type – mengmeng Sep 12 '19 at 11:53
  • Yes, that one is for output... Have a look at the `DateTimeFormatter` used in `OffsetDateTime.parse(...)`. You can use `DateTimeFormatter.ISO_OFFSET_DATE_TIME` there, too. Your date time `String` represents a time calculated by an offset from UTC. You can convert is to a `LocalDateTime`, if you want to. – deHaar Sep 12 '19 at 11:54