Using SimpleDateFormat's "ZZZZZ" (+03:00) for timezone before Android 4.3
Asked Answered
B

3

6

I'm using SimpleDateFormat with format yyyy-MM-dd'T'HH:mm:ssZZZZZ. The expected output is "2014-08-26T13:00:14+03:00". However, "ZZZZZ" is only supported since Android 4.3.

Here is the result:

  • Above 4.3: 2014-08-26T13:00:14+03:00
  • Below 4.3: 2014-08-26T13:00:14+0300

Note that the timezone section is different (03:00 vs 0300)

I have looked at this bug report: http://code.google.com/p/android/issues/detail?id=73209.

Is it possible to get same timezone format in all version like above 4.3? Any suggestions?

Benford answered 3/9, 2014 at 5:35 Comment(6)
Where is the difference between both values?Divagate
@denize I can not see any difference. aybe you copied wrong value?Divagate
What is depending on this? Could you use the "Wrong" -version every time by using format: "yyyy-MM-dd'T'HH:mm:ssZ"?Individually
i need 2014-08-26T13:00:14+03:00 this format always any option?Benford
Unfortunately, as stated in the bug report, this is not a bug. It's working as intended since Android 4.3 below doesn't support ZZZZZ. "Five-count patterns (such as "MMMMM") used for the shortest non-numeric representation of a field were introduced in Android 4.3 (Jelly Bean MR2, API level 18).". I'll try to edit your question to specify your issue.Thekla
From the example given the difference is ":" missing in pre 4.3, what I would do is create a wrapper function that will return date in requested format and for versions less than 4.3 I would modify string to inject missing ":" character.Abstinence
T
1

This is what I use, no version check:

String result = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZZZZZ").format(calendar.getTime());
//fix up for older Android where ZZZZZ does not include colon
if (!result.substring(result.length() - 3).startsWith(":")) {
    result = result.substring(0, result.length() - 2) + ":" + result.substring(result.length() - 2);
        }
return result;
Translunar answered 8/12, 2016 at 1:13 Comment(0)
H
1

you can try this format: ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")

Herr answered 8/7, 2019 at 8:50 Comment(0)
S
0

This is the sort of fix I use.

private static final SimpleDateFormat DATEFORMATRFC3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.ENGLISH);

public static String formatrfc3339(Date date) {
    if(VersionUtils.isJellyBeanMR2Compatible()) {
        return DATEFORMATRFC3339.format(date);
    }
   return DATEFORMATRFC3339.format(date).replaceAll("(\\d\\d)(\\d\\d)$", "$1:$2");
}
Susannesusceptibility answered 28/5, 2015 at 10:56 Comment(2)
Of cause is VersionUtils.isJellyBeanMR2Compatible() easy to implement, but you should have added this to your answer.Ablution
Just a second note: Does RFC3339 not require milliseconds? -> yyyy-MM-dd'T'HH:mm:ss.SSSZZZZAblution

© 2022 - 2024 — McMap. All rights reserved.