日历getTimeInMillis 1小时差 [英] Calendar getTimeInMillis difference of 1 hour

查看:723
本文介绍了日历getTimeInMillis 1小时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这在UI有一个的DatePicker TimePicker 的应用程序。我需要得到由在服务器中的用户和存储设置的日期和时间。

例如用户选择二零一五年十一月十三日13:00,而我的仿真器的时区是GMT独立的时间设置为GMT + 8,返回 timeInSec 区应该是1447390800,但它原来是1447387200,1小时的差异。最终从服务器也是不对收到我显示的时间。

为什么会这样呢?事做在GMT时区的国家夏令还是什么都我在code错呢?在我的国家,没有夏令..

下面是我的code:

 日历CAL = Calendar.getInstance();
cal.set(mDatePicker.getYear(),mDatePicker.getMonth(),mDatePicker.getDayOfMonth(),mTimePicker.getCurrentHour(),mTimePicker.getCurrentMinute(),0);
//获得独立时区和LT的时间(秒); - 对二零一五年十一月十四日更新:这是错误的!
。长期的timezoneoffset = cal.getTimeZone()的getOffset(cal.getTimeInMillis());
长timeInSec =((cal.getTimeInMillis()+的timezoneoffset)/ 1000);

更新2015年11月11日23:03
再次直通的code检查后,我发现这是我TimePicker给错误的值。正如马蒂亚MAESTRINI的要求,这里是我的codeS创建时间选择器:
https://dl.dropboxusercontent.com/u/4256111/AddApptDialogFragment.java

更新2015年11月12日07:14
结案。它是COS的TimePicker的。请考虑为俺们的注释。

更新2015年11月14日23:17
OMG ...... Calendar.currentTimeMillis(),其实是给予时间独立时区(即UTC)!
此链接(<一个href=\"http://stackoverflow.com/questions/9867254/java-timezone-why-different-timezone-give-same-value-in-millisec\">Java:时区为什么不同时区在毫秒给出相同的价值)是如此的误导!
这是SHLD什么üSHLD在看:的http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()

所以我SHLD只是这样做的:

 日历CAL = Calendar.getInstance();
cal.set(mDatePicker.getYear(),mDatePicker.getMonth(),mDatePicker.getDayOfMonth(),mTimePicker.getCurrentHour(),mTimePicker.getCurrentMinute(),0);
长timeInSec =(cal.getTimeInMillis()/ 1000);


解决方案

如果你愿意,你可以直接使用UTC时间戳 getTimeInMillis()


  

众长getTimeInMillis()


  
  

返回:当前时间从UTC时代毫秒


  
  

参考:结果
  http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()


例如设置日历日期 2015年11月13日13:00 与GMT + 8时区:

 日历日历= Calendar.getInstance();
calendar.set(2015,Calendar.NOVEMBER,13,13,0,0);
INT timeInSec = calendar.getTimeInMillis()/ 1000;

timeInSec 的值为 1447390800

修改

什么是你在这个片段中code的获得输出?

 日历日历= Calendar.getInstance();
calendar.set(2015,Calendar.NOVEMBER,13,13,0,0);
诠释偏移量= calendar.get(Calendar.ZONE_OFFSET)+ calendar.get(Calendar.DST_OFFSET);
长utcTimeInMillis = calendar.getTimeInMillis()+偏移;日历utcCalendar = Calendar.getInstance(Locale.ENGLISH);
utcCalendar.setTimeInMillis(utcTimeInMillis);Log.d(TAG,时间:+ calendar.getTime());
Log.d(TAGTimeInMillis:+ calendar.getTimeInMillis());
Log.d(TAG,显示名称:+ calendar.getTimeZone()getDisplayName());
Log.d(TAG,偏移+偏移量);
Log.d(TAG,UTC时间:+ utcCalendar.getTime());
Log.d(TAG,UTC TimeInMillis:+ utcTimeInMillis);

这是我得到的结果:

 时间:周五11月13日13:00:00 GMT + 08:00 2015年
TimeInMillis:1447390800061
显示名称:香港标准时间
偏移:28800000
UTC时间:周五11月13日21:00:00 GMT + 08:00 2015年
UTC TimeInMillis:1447419600061

I am writing an app which has a DatePicker and a TimePicker in the UI. I need to get the date and time set by the user and store in the server.

For example user chose "13 Nov 2015 13:00", and the timezone of my emulator is set as GMT+8, the returned timeInSec in GMT independent of time zone should be "1447390800", but it turns out to be "1447387200", a difference of 1 hour. End up my displayed time received from server also wrong.

Why is it so? Something to do with daylight savings in GMT timezone countries or what have I done wrongly in the code? In my country there is no daylight savings..

Here is my code:

Calendar cal = Calendar.getInstance();
cal.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth(), mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute(), 0);
// get time in seconds independent of timezone <- update on 2015/11/14: this is wrong!! 
long timezoneOffset = cal.getTimeZone().getOffset(cal.getTimeInMillis());
long timeInSec = ((cal.getTimeInMillis() + timezoneOffset)/1000);

Update 2015/11/11 23:03 After checking thru the code again, i found it is my TimePicker giving the wrong value. As request by Mattia Maestrini, here is my codes that creates the Time Picker: https://dl.dropboxusercontent.com/u/4256111/AddApptDialogFragment.java

Update 2015/11/12 07:14 Case closed. It is cos of the TimePicker. Pls look into the comment for the ans.

Update 2015/11/14 23:17 Omg... Calendar.currentTimeMillis() actually gives time independent of timezone (i.e. UTC)! This link (Java: Timezone why different timezone give same value in millisec) is so misleading! This shld be what u shld be looking at: http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()

So I shld be just doing this:

Calendar cal = Calendar.getInstance();
cal.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth(), mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute(), 0);
long timeInSec = (cal.getTimeInMillis()/1000);

解决方案

If you want the UTC timestamp you can directly use getTimeInMillis()

public long getTimeInMillis()

Returns: the current time as UTC milliseconds from the epoch.

Reference:
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()

For example set the calendar date to 13 Nov 2015 13:00 with a GMT+8 timezone:

Calendar calendar = Calendar.getInstance();
calendar.set(2015, Calendar.NOVEMBER, 13, 13, 0, 0);
int timeInSec = calendar.getTimeInMillis() / 1000;

the value of timeInSec is 1447390800

EDIT

What is the output you obtain with this snippet of code?

Calendar calendar = Calendar.getInstance();
calendar.set(2015, Calendar.NOVEMBER, 13, 13, 0, 0);
int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
long utcTimeInMillis = calendar.getTimeInMillis() + offset;

Calendar utcCalendar = Calendar.getInstance(Locale.ENGLISH);
utcCalendar.setTimeInMillis(utcTimeInMillis);

Log.d(TAG, "Time: " + calendar.getTime());
Log.d(TAG, "TimeInMillis: " + calendar.getTimeInMillis());
Log.d(TAG, "DisplayName: " + calendar.getTimeZone().getDisplayName());
Log.d(TAG, "Offset: " + offset);
Log.d(TAG, "UTC Time: " + utcCalendar.getTime());
Log.d(TAG, "UTC TimeInMillis: " + utcTimeInMillis);

This is the result I obtained:

Time: Fri Nov 13 13:00:00 GMT+08:00 2015
TimeInMillis: 1447390800061
DisplayName: Hong Kong Standard Time
Offset: 28800000
UTC Time: Fri Nov 13 21:00:00 GMT+08:00 2015
UTC TimeInMillis: 1447419600061

这篇关于日历getTimeInMillis 1小时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆