I have this date string : 2016-04-26T09:14:10.477Z
which is in UTC timezone. I want to convert it to the user local Timezone, so if it is GMT +02:00, I want a date with a value of 2016-04-26T11:14:10.477Z
(note the 9 changes to 11).
I've been using this so far :
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
val now = Date()
val limit = sdf.parse(coupon.usedAt)
Here limit = Tue Apr 26 09:14:10 GMT+02:00 2016
which is not correct I suppose.
So how can I convert it correctly? Thanks!
Edit : my question differs from this one, since I need to use the object SimpleDateFormat
and not the Date
one
Try this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
The question was in Kotlin and I see all the answers are written in Java, so here is the Kotlin solution
val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ", Locale.getDefault())
formatter.timeZone = TimeZone.getTimeZone("UTC")
val result = formatter.parse(dateAsString)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With