J.W*_*J.W 5 java time android offset
我发现了一些类似的问题,例如:
如何从Android设备获取格林威治标准时间(如格林威治标准时间+7:00)的时区偏移量?
但是现在所有这些答案(+12:00)对于新西兰夏令时都是不正确的。
当我进行调试时,我从 Google 日历事件对象中得到了这个:
"dateTime" -> "2016-11-06T10:00:00.000+13:00"
那么如何获得正确的偏移量+13:00呢?
谢谢。
要以毫秒为单位获取与 UTC 的当前偏移量(可能因 DST 而异):
return TimeZone.getDefault().getOffset(System.currentTimeMillis());
Run Code Online (Sandbox Code Playgroud)
要获得 RFC 822 时区字符串,您只需创建一个 SimpleDateFormat 实例:
return new SimpleDateFormat("Z").format(new Date());
Run Code Online (Sandbox Code Playgroud)
格式为 (+/-)HHMM
小智 0
So, I tried to get gmt offset through Calendar and SimpleDateFormat but both returns 0. I found the solution using deprecated methods in Date class. So, this code works for me.
private double getOffset() {
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
int defHour = calendar.get(Calendar.HOUR_OF_DAY);
int defMinute = calendar.get(Calendar.MINUTE) + (defHour * 60);
Date date = new Date(System.currentTimeMillis());
int curHour = date.getHours();
int curMinute = date.getMinutes() + (curHour * 60);
double offset = ((double) curMinute - defMinute) / 60;
return offset > 12? -24 + offset : offset;
}
Run Code Online (Sandbox Code Playgroud)
Then you can format a result
| 归档时间: |
|
| 查看次数: |
4709 次 |
| 最近记录: |