How to convert UTC time to another time zone ("CST", "IST")

In my Android-application server will return a certain date UTCin the following format ( yyyy-MM-dd HH:mm:ss) for 24 hours, and I need to convert this time in the TimeZone user, for example CST, IST.

I made the following code, but I know if this is correct or not, please help me make the correct time zone change.

I get UTC date as json string and convert to user timezone format and show Android side

private static Date utcDate;
private static DateFormat expireFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");

        try {
            expireFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            utcDate = expireFormat.parse("2014-04-01 10:32:00");
            System.out.println(TimeZone.getDefault().getID());
            expireFormat.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
            System.out.println(expireFormat.format(utcDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    output of the code:
    Asia/Calcutta
    2014-04-01 16:02:00
+4
source share
1 answer

- , , .

,

  • SimpleDateFormat . !

  • # 1 Date.

  • "" ? ( )? ? TimeZone - . Olson Alias, .

  • ? , , , API Java 8 DateTime.

+4

Source: https://habr.com/ru/post/1534305/


All Articles