Daniel Imberman
Daniel Imberman

Reputation: 628

OffsetDateTime time is changing zone offset

So I have a snippet of code that should basically take in a date-time string and return it in EXACTLY the same format

val dateString = "2016-01-01T01:30:55.000+00:00"
println("before: " + dateString)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS+hh:mm")
val date = OffsetDateTime.parse(dateString)
println("after: " + date.format(formatter))

However, when I try to re-convert it to a string, I'm getting a different offset!

before: 2016-01-01T01:30:55.000+00:00
after:  2016-01-01T01:30:55.000+01:30

Is there something I'm doing wrong here?

Thank you!

Upvotes: 1

Views: 1472

Answers (2)

JodaStephen
JodaStephen

Reputation: 63395

The +hh:mm is not the correct format for a time-zone offset. You should use XXX instead, as per the documentation.

Upvotes: 3

Matthieu Saleta
Matthieu Saleta

Reputation: 1508

The default DateTimeFormatter for OffsetDateTime.parse(CharSequence text)is yyyy-MM-dd'T'HH:mm:ss+hh:mm (without ".SSS").

So I think your error is there.

Pass the formatter variable as the 2nd parameter of your method and it should work.

Upvotes: 0

Related Questions