2016-06-21 79 views
-1

我有个这样的日子星期二六月21 14:47:37 GMT + 05:30 2016,这是我自己创建的。我使用日历创建它。用户选择日期并将其保存为日历中的毫秒数。 当我把它,把它,我再创建一个日历实例把保存好的毫秒到它,并得到这样的日期:Android时区问题,困惑

Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeZone(TimeZone.getDefault()); 
    calendar.setTimeInMillis(SSPreferences.getDate()); 

现在,而从日历中获取日期我这样做:

calendar.getTime()//This is what I send to server. 

我把它发送到服务器,但是当服务器给我发送日期时,它总是在我的时间前5.5小时。

我知道现在的时间是格林威治标准时间+5:50。那么服务器在做什么? 如何发送日期,以便我回到我发送给服务器的相同日期。

任何帮助表示赞赏。 感谢

+0

获取时间UTC并保持服务器或毫秒 – Sreekanth

+0

“这是我发送给服务器的。” - *你如何*将它发送到服务器? –

+0

如何将日期发送到服务器?你使用httpurlconnection将它发送到php脚本吗?还有别的吗? –

回答

0

检查这个

static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss" 

public static Date GetUTCdatetimeAsDate() 
{ 
    //note: doesn't check for null 
    return StringDateToDate(GetUTCdatetimeAsString()); 
} 

public static String GetUTCdatetimeAsString() 
{ 
    final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT); 
    sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 
    final String utcTime = sdf.format(new Date()); 

    return utcTime; 
} 

public static Date StringDateToDate(String StrDate) 
{ 
    Date dateToReturn = null; 
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT); 

    try 
    { 
     dateToReturn = (Date)dateFormat.parse(StrDate); 
    } 
    catch (ParseException e) 
    { 
     e.printStackTrace(); 
    } 

    return dateToReturn; 
} 
0

最后我通过添加/从本地时区中减去rawOffSet解决的问题。

TimeZone timeZone = TimeZone.getDefault(); 
    int offset = timeZone.getOffset(SSPreferences.getDate()); 
    WriteLog.Print("offset is "+offset); 
    long newtime = SSPreferences.getDate() + offset; 
    calendar.setTimeInMillis(newtime);