Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert UTC date to locale GMT time on android

Tags:

android

kotlin

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

like image 476
tufekoi Avatar asked Apr 26 '16 09:04

tufekoi


2 Answers

Try this

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
like image 111
Praween Kumar Mishra Avatar answered Oct 16 '22 20:10

Praween Kumar Mishra


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)
like image 34
Alaa Eddine Cherbib Avatar answered Oct 16 '22 18:10

Alaa Eddine Cherbib