如何从android设备获取格林尼治标准时间偏移量(如GMT + 7:00)?

我正在使用此代码获取Android设备的timezone

 TimeZone tz = TimeZone.getDefault(); String current_Time_Zone = (TimeZone.getTimeZone(tz.getID()).getDisplayName( false, TimeZone.SHORT)) 

但它总是返回像“ IST ”的时区,但我想要在GMT GMT+7:00. GMT得到时区GMT+7:00.

Solutions Collecting From Web of "如何从android设备获取格林尼治标准时间偏移量(如GMT + 7:00)?"

这可能会给你一个关于如何实现它的想法:

 Calendar mCalendar = new GregorianCalendar(); TimeZone mTimeZone = mCalendar.getTimeZone(); int mGMTOffset = mTimeZone.getRawOffset(); System.out.printf("GMT offset is %s hours", TimeUnit.HOURS.convert(mGMTOffset, TimeUnit.MILLISECONDS)); 

(TimeUnit是“java.util.concurrent.TimeUnit”)

此代码返回我GMT时间偏移量。

 Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault()); Date currentLocalTime = calendar.getTime(); DateFormat date = new SimpleDateFormat("Z"); String localTime = date.format(currentLocalTime); 

它将返回如下所示的时区偏移: +0530

如果我们使用下面的SimpleDateFormat

 DateFormat date = new SimpleDateFormat("z",Locale.getDefault()); String localTime = date.format(currentLocalTime); 

它将返回时区偏移,如下所示: GMT + 05:30

这里是以GMT + 05:30格式获取时区偏移的解决scheme

 public String getCurrentTimezoneOffset() { TimeZone tz = TimeZone.getDefault(); Calendar cal = GregorianCalendar.getInstance(tz); int offsetInMillis = tz.getOffset(cal.getTimeInMillis()); String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60)); offset = "GMT"+(offsetInMillis >= 0 ? "+" : "-") + offset; return offset; } 
 public static String timeZone() { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault()); String timeZone = new SimpleDateFormat("Z").format(calendar.getTime()); return timeZone.substring(0, 3) + ":"+ timeZone.substring(3, 5); } 

+03:30一样返回

 public static String getCurrentTimezoneOffset() { TimeZone tz = TimeZone.getDefault(); Calendar cal = GregorianCalendar.getInstance(tz); int offsetInMillis = tz.getOffset(cal.getTimeInMillis()); String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60)); offset = (offsetInMillis >= 0 ? "+" : "-") + offset; return offset; } 
  TimeZone tz = TimeZone.getDefault(); Calendar cal = GregorianCalendar.getInstance(tz); int offsetInMillis = tz.getOffset(cal.getTimeInMillis()); String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60)); offset = (offsetInMillis >= 0 ? "+" : "-") + offset; 

另一个解决时区偏移的方法是:

 TimeZone tz = TimeZone.getDefault(); String current_Time_Zone = getGmtOffsetString(tz.getRawOffset()); public static String getGmtOffsetString(int offsetMillis) { int offsetMinutes = offsetMillis / 60000; char sign = '+'; if (offsetMinutes < 0) { sign = '-'; offsetMinutes = -offsetMinutes; } return String.format("GMT%c%02d:%02d", sign, offsetMinutes/60, offsetMinutes % 60); } 

一个解决scheme是使用Z符号,如:

 new SimpleDateFormat(pattern, Locale.getDefault()).format(System.currentTimeMillis()); 

pattern可能是:

  • Z / ZZ / ZZZ:-0800
  • ZZZZ:GMT-08:00
  • ZZZZZ:-08:00

全文请参考:

http://developer.android.com/reference/java/text/SimpleDateFormat.html

你可以这样做:

  TimeZone tz = TimeZone.getDefault(); int offset = tz.getRawOffset(); String timeZone = String.format("%s%02d%02d", offset >= 0 ? "+" : "-", offset / 3600000, (offset / 60000) % 60); 
 TimeZone tz = TimeZone.getDefault(); String gmt1=TimeZone.getTimeZone(tz.getID()) .getDisplayName(false,TimeZone.SHORT); String gmt2=TimeZone.getTimeZone(tz.getID()) .getDisplayName(false,TimeZone.LONG); Log.d("Tag","TimeZone : "+gmt1+"\t"+gmt2); 

看看这是否有帮助:)

如果有人正在寻找如何代表GMT作为代表小时偏移的浮点数
(例如“GMT-0530”到-5.5),你可以使用这个:

 Calendar calendar = new GregorianCalendar(); TimeZone timeZone = calendar.getTimeZone(); int offset = timeZone.getRawOffset(); long hours = TimeUnit.MILLISECONDS.toHours(offset); float minutes = (float)TimeUnit.MILLISECONDS.toMinutes(offset - TimeUnit.HOURS.toMillis(hours)) / MINUTES_IN_HOUR; float gmt = hours + minutes; 
 TimeZone timeZone = TimeZone.getDefault(); String timeZoneInGMTFormat = timeZone.getDisplayName(false,TimeZone.SHORT); 

输出:GMT + 5:30

我也一直在看这个,并试图解决如何应用时区和DST。 这是我的代码。

  public long applyGMTOffsetDST(long time) { // Works out adjustments for timezone and daylight saving time Calendar mCalendar = new GregorianCalendar(); TimeZone mTimeZone = mCalendar.getTimeZone(); boolean dstBool = mTimeZone.getDefault().inDaylightTime( new Date() ); // add an hour if DST active if (dstBool == true) { time = time + secondsPerHour; } // add offest hours int mGMTOffset = mTimeZone.getRawOffset(); if (mGMTOffset > 0) { long offsetSeconds = secondsPerHour * mGMTOffset; time = time + offsetSeconds; } return time; } 

这似乎工作,但有没有更好的方式来获得实际的时间,从而代表了一个对用户有意义且准确的时间?

我在使用ZoneDateTime类的Java8(非Android)中偶然发现了一个简单的解决scheme。 可能有其他类实现TemporalAccessor接口工作,但我没有find它们。 据我所知,这将不会使用标准的DateDateTimeLocalDateTimeCalender类。

  ZoneOffset myOffset = ZonedDateTime.now().getOffset(); ZoneOffset myOffset2 = ZoneOffset.from(ZonedDateTime.now()); log.info("ZoneOffset is " + myOffset.getId()); // should print "+HH:MM" log.info("ZoneOffset2 is " + myOffset2.getId()); // should print "+HH:MM" 

这个解决scheme的好处是,它避免了大量的模math,string生成和parsing。

TimeZone timeZone = TimeZone.getTimeZone(“GMT + 7:00”);

明确声明。