-1

In my app I am getting time from server in API in IST timezone, I want to show time in device's local time zone.

Below is my code for this but it seems its not working.

SimpleDateFormat serverSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat localSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
serverSDF.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
localSDF.setTimeZone(TimeZone.getDefault());

Date serverDate = serverSDF.parse(dateString);
String utcDate = utcSDF.format(serverDate);
Date localDate = localSDF.parse(utcDate);

From server I am getting time "2018-02-28 16:04:12" in IST and the code above displays "Wed Feb 28 10:34:12 GMT+05:30 2018".

Community
  • 1
  • 1
  • 2
    If possible, modify your server to send GMT format. And in client side convert accordingly. Else, get in IST format, convert to GMT and then convert to local timezone. https://www.journaldev.com/696/how-to-convert-java-date-into-specific-timezone-format – Gokul Nath KP Feb 28 '18 at 11:00
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Feb 28 '18 at 11:47
  • Possible duplicate of [Timezone conversion](https://stackoverflow.com/questions/6567923/timezone-conversion) – Ole V.V. Feb 28 '18 at 11:50
  • 1
    Please search before posting your question and find a good answer faster. This question has been asked and answered with minor variations over and over. – Ole V.V. Feb 28 '18 at 11:51

2 Answers2

3

The other answer uses GMT+05:30, but it's much better to use a proper timezone such as Asia/Kolkata. It works now because India currently uses the +05:30 offset, but it's not guaranteed to be the same forever.

If someday the government decides to change the country's offset (which already happened in the past), your code with a hardcoded GMT+05:30 will stop working - but a code with Asia/Kolkata (and a JVM with the timezone data updated) will keep working.

But today there's a better API to manipulate dates, see here how to configure it: How to use ThreeTenABP in Android Project

This is better than SimpleDateFormat, a class known to have tons of problems: https://eyalsch.wordpress.com/2009/05/29/sdf/

With this API, the code would be:

String serverDate = "2018-02-28 16:04:12";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime istLocalDate = LocalDateTime.parse(serverDate, fmt);

// set the date to India timezone
String output = istLocalDate.atZone(ZoneId.of("Asia/Kolkata"))
    // convert to device's zone
    .withZoneSameInstant(ZoneId.systemDefault())
    // format
    .format(fmt);

In my machine, the output is 2018-02-28 07:34:12 (it varies according to the default timezone of your environment).

Although it seems complicated to learn a new API, in this case I think it's totally worth it. The new API is much better, easier to use (once you learn the concepts), less error-prone, and fix lots of problems of the old API.

Check Oracle's tutorial to learn more about it: https://docs.oracle.com/javase/tutorial/datetime/

istt
  • 31
  • 1
0

Update: Check this answer by @istt which uses modern Java8 date-time api.

You don't need to change format in UTC first. You can simply use:

SimpleDateFormat serverSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat localSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
serverSDF.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
localSDF.setTimeZone(TimeZone.getDefault());

String localDate = localSDF.format(serverSDF.parse(dateString));
global_warming
  • 833
  • 1
  • 7
  • 11
  • The terrible classes used here are long obsolete, supplanted years ago by the modern *java.time* classes defined in JSR 310. See the correct solution in the [Answer by istt](https://stackoverflow.com/a/49029315/642706). – Basil Bourque Jul 12 '20 at 02:03
  • @BasilBourque you are correct, use Java8 classes, they are modern and immutable, use them. – global_warming Jul 15 '20 at 12:16