1

I have this start datetime: "20201023T200457Z" (it seem to be UTC0000) how can I convert/generate it with this "yyyyMMdd HH:mm:ss" pattern in local time on a mobile?

I get this result: Fri Oct 23 20:04:57 GMT+02:00 2020 with this code:

    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.getDefault());
    Date startGMTInput = df.parse(start);
    Log.e(TAG, "start: " + startGMTInput.toString());// -> Fri Oct 23 20:04:57 GMT+02:00 2020

But my target is to get: 2020-10-23 22:04:57 //because I'm in GMT+2 timezone

froniq
  • 105
  • 1
  • 8
  • 1
    Well, you can use `SimpleDateFormat` to parse _and_ format dates so just create another one with the pattern you want. – Thomas Oct 22 '20 at 22:18
  • 1
    Use `java.time`. – MC Emperor Oct 22 '20 at 22:55
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Oct 23 '20 at 02:09

2 Answers2

1

java.time either through desugaring or ThreeTenABP

Consider using java.time, the modern Java date and time API, for your date and time work. Let’s first define the formatters we need:

private static final DateTimeFormatter inputFormatter
        = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmssX");
private static final DateTimeFormatter outputFormatter
        = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");

( DateTimeFormatter is thread-safe, so we can safely declare them static.) Do the time zone conversion explicitly:

    String startString = "20201023T200457Z";
    Instant start = inputFormatter.parse(startString, Instant.FROM);
    String target = start.atZone(ZoneId.systemDefault()).format(outputFormatter);
    System.out.println(target);

Output in my time zone (currently at offset +02:00 like yours):

20201023 22:04:57

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. Only in this case use the method reference Instant::from instead of the constant Instant.FROM.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks for the great ideas and links! Finally I found a solution without to use java8 and external libs to backward compatibility. First I converted input format with SimpleDateFormat, then set the timezone to GMT, and another SimpleDateFormat I converted it with the out format. – froniq Oct 24 '20 at 14:33
0

Set your timezone to GMT+2 before any date operations.

isoFormat.setTimeZone(TimeZone.getTimeZone("GMT+2"));
  • I have doubts whether it will help. Could you demonstrate with a complete example that it will? And/or even better, explain in more detail how? – Ole V.V. Oct 23 '20 at 03:27