Using SimpleDateFormat's “ZZZZZ” (+03:00) for timezone before Android 4.3

痞子三分冷 提交于 2019-12-23 13:19:16

问题


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?


回答1:


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");
}



回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/25636917/using-simpledateformats-zzzzz-0300-for-timezone-before-android-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!