2016-06-21 81 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);