0
  fun getFormatFromISO(iso: String, pattern: String): String {
    var res: String? = ""
    val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH)
    formatter.timeZone= TimeZone.getTimeZone("UTC")
    return try {
        val date = formatter.parse(iso)
      val timeFormat = SimpleDateFormat(pattern, Locale.getDefault())
        timeFormat.timeZone= TimeZone.getDefault() // to convert to devices timezone
        res=timeFormat.format(date)
        res
    } catch (var6: ParseException) {
      "ERR"
    }
  }

I am trying to change the Api's response variable to current device's timestamp so that it will be used as some "textview.text". The part where "TimeZone.getDefault()" is used, it is suppose to change the UTC time-zone to device's time zone.But it is not changing, instead it is parsing UTC converted time.

Passing "H", "mm" in the parameter 'pattern', passing something like "2021-10-26T07:22:37Z" in the parameter'iso'

the result is :

date val is 'Tue Oct 26 06:27:18 EDT' res is 06 || res is 27

Required result is res value to be in Device's Time Zone.

1 Answers1

1

The part where "TimeZone.getDefault()" is used, it is suppose to change the UTC time-zone to device's time zone.

Your understanding is wrong. TimeZone.getDefault() returns the TimeZone of the server, not the device.

Solution:

There are many web services (some links have been given in the following list) using which you can get the timezone where the device is. Once you have the timezone, you can use that in your code.

  1. How to get Latitude and Longitude of the mobile device in android?
  2. How to get a time zone from a location using latitude and longitude coordinates?
  3. Android get device locale

Switch to java.time:

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Demo:

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getFormattedDateTimeInDeviceTz("2021-10-26T18:12:20.698134Z", "uuuu-MM-dd'T'HH:mm:ssXXX"));
    }

    static String getFormattedDateTimeInDeviceTz(String iso, String pattern) {
        Instant instant = Instant.parse(iso);

        // Get the date-time in the device's timezone e.g. Asia/Kolkata
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH);
        return instant.atZone(ZoneId.of("Asia/Kolkata")).format(dtf);
    }
}

Output:

2021-10-26T23:42:20+05:30

ONLINE DEMO

Note: I have used Locale.ENGLISH in the demo code. Change it using the third link in the list above. Note that Locale.getDefault() returns the Locale of the server, not the device.

Learn more about the modern Date-Time API from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110