Android手機中取得當前時區(以GMT形式)
阿新 • • 發佈:2018-11-13
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Android手機中如果想以GMT形式(GMT+08:00)得到當前時區,可以參照如下程式碼所示:
public static String getCurrentTimeZone () { TimeZone tz = TimeZone.getDefault(); return createGmtOffsetString(true,true,tz.getRawOffset()); } public static String createGmtOffsetString(boolean includeGmt, boolean includeMinuteSeparator, int offsetMillis) { int offsetMinutes = offsetMillis / 60000; char sign = '+'; if (offsetMinutes < 0) { sign = '-' ; offsetMinutes = -offsetMinutes; } StringBuilder builder = new StringBuilder(9); if (includeGmt) { builder.append("GMT"); } builder.append(sign); appendNumber(builder, 2, offsetMinutes / 60); if (includeMinuteSeparator) { builder.append(':'); } appendNumber(builder, 2, offsetMinutes % 60); return builder.toString(); } private static void appendNumber(StringBuilder builder, int count, int value) { String string = Integer.toString(value); for (int i = 0; i < count - string.length(); i++) { builder.append('0'); } builder.append(string); }
關於GMT和UTC的更多內容請參考 http://blog.csdn.net/hudashi/article/details/7069600