当前位置:   article > 正文

Android手机中取得当前时区(以GMT形式)_android 获取当前时区值中国时区是+8

android 获取当前时区值中国时区是+8

Android手机中如果想以GMT形式(GMT+08:00)得到当前时区,可以参照如下代码所示:

  1. public static String getCurrentTimeZone()
  2. {
  3. TimeZone tz = TimeZone.getDefault();
  4. return createGmtOffsetString(true,true,tz.getRawOffset());
  5. }
  6. public static String createGmtOffsetString(boolean includeGmt,
  7. boolean includeMinuteSeparator, int offsetMillis) {
  8. int offsetMinutes = offsetMillis / 60000;
  9. char sign = '+';
  10. if (offsetMinutes < 0) {
  11. sign = '-';
  12. offsetMinutes = -offsetMinutes;
  13. }
  14. StringBuilder builder = new StringBuilder(9);
  15. if (includeGmt) {
  16. builder.append("GMT");
  17. }
  18. builder.append(sign);
  19. appendNumber(builder, 2, offsetMinutes / 60);
  20. if (includeMinuteSeparator) {
  21. builder.append(':');
  22. }
  23. appendNumber(builder, 2, offsetMinutes % 60);
  24. return builder.toString();
  25. }
  26. private static void appendNumber(StringBuilder builder, int count, int value) {
  27. String string = Integer.toString(value);
  28. for (int i = 0; i < count - string.length(); i++) {
  29. builder.append('0');
  30. }
  31. builder.append(string);
  32. }

关于GMT和UTC的更多内容请参考 http://blog.csdn.net/hudashi/article/details/7069600

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/294944
推荐阅读
相关标签
  

闽ICP备14008679号