-1

How can I parse this code to localDateTime?

I have 2 area that give me localDateTime, One of them is 2023-02-22T09:47:00.5371934+03:00 the other one is 2023-02-22T09:47:00.537 and response like this:

java.time.format.DateTimeParseException: Text '2023-02-22T09:47:00.537' could not be parsed at index 23 \n\tat org.apache.cxf.jaxws.JaxWsClientProxy.mapException(JaxWsClientProxy.java:195)

I have tried DateTimeFormatter.ISO_ZONED_DATE_TIME, it didn't work.

Do u you have any suggestions?

hsynuls
  • 21
  • 3
  • 2
    share your code – kerbermeister Feb 22 '23 at 08:39
  • 4
    These are different formats. One is a `ZonedDateTime`, the other is a `LocalDateTime`. You need to parse them using different formats. – f1sh Feb 22 '23 at 08:41
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 22 '23 at 16:11
  • You should hardly want to process any of those as `LocalDateTime`. If you know from which 2 areas they come, then the `ZonedDateTime` class that @f1sh refers to is probably right, but will require a (different) conversion for each. So for starters, from which two areas do they come? – Ole V.V. Feb 23 '23 at 19:35
  • 1
    yes, I tried different conversion for each areas and the problem is solved thank you @f1sh – hsynuls Feb 24 '23 at 11:02

1 Answers1

2

OffsetDateTime

Your first input has an offset of three hours ahead of UTC. So parse as an OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( "2023-02-22T09:47:00.5371934+03:00" ) ;

LocalDateTime

This other input has no offset, and no time zone. So parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( "2023-02-22T09:47:00.537" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154