2013-04-02 2 views
1

어떤 이유로 나는 타임 스탬프를 올바르게 표시하는 데 항상 어려움을 겪고 있지만 어쨌든 여기에 내 문제가 있습니다.UTC 타임 스탬프를 로컬 장치 타임 스탬프로 변환

Calendar Provider API에서 이벤트를 가져 오는 중이며 US Holidays 캘린더 이벤트와 같은 일부 이벤트는 UTC이므로 타임 스탬프가 장치에 있어야하는 것이 아닙니다 (장치가 해당 시간대에 있지 않은 경우).

나는이 1374105600000의 타임 스탬프가 07/18/2013 00:00:00 UTC이므로 7 월 18 일 UTC 자정에 있습니다. 내가 원하는 것은 로컬 장치 시간 자정에 7 월 18 일의 타임 스탬프입니다. 나는 UTC 타임 스탬프

long local = time+offset; 

이 나에게 잘못된 시간을 제공이를 추가하는 경우

내가

Calendar cal = Calendar.getInstance(); 
TimeZone tz = cal.getTimeZone(); 
cal.setTimeInMillis(time); 

long offset = tz.getRawOffset(); //gives me -18000000 5hr difference since I am in EST which I think is actually wrong because it should be a 4hr difference now with DST 

그래서 할 것입니다 july 17th at 3:00PM

나는 시간을 빼면

long local = time-offset; 

나는 아직도 잘못된 시간을 얻는다. 그것은 나를 july 18th at 1:00AM하지만 나에게 준다고 생각하지 않는다. 왜냐하면 사람들을 위해 일하지 않기 때문에 심지어 빼기도해야한다고 생각한다. + 시간대 차이.

무엇이 잘못 되었나요? 정확한 시간을 얻기 위해 올바른 오프셋을 얻을 수없는 이유는 무엇입니까?

나 또한 너무 Link

답변

0

흠 .. 코드가 아래 시나리오를 반전 참고로 같은이를 사용했다,하지만 어쩌면 u는 그것을 사용할 수있다? 자바 does not 이후

private Date cvtToGmt(Date date) 
{ 
    TimeZone tz = TimeZone.getDefault(); 
    Date ret = new Date(date.getTime() - tz.getRawOffset()); 

    // if we are now in DST, back off by the delta. Note that we are checking the GMT date, this is the KEY. 
    if (tz.inDaylightTime(ret)) 
    { 
     Date dstDate = new Date(ret.getTime() - tz.getDSTSavings()); 

     // check to make sure we have not crossed back into standard time 
     // this happens when we are on the cusp of DST (7pm the day before the change for PDT) 
     if (tz.inDaylightTime(dstDate)) 
     { 
     ret = dstDate; 
     } 
    } 

    return ret; 
} 
2

변환을 수행하는 Date 객체와의 약간의 이상을 Timezone 정보를 연결합니다. 나는 "EST"

import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 

public class TimeZoneTest { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Calendar gmtTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
     Calendar estTime = Calendar.getInstance(TimeZone.getTimeZone("America/New_York")); 

     System.out.println(getInputDate() + " (Actually GMT)"); 
     estTime.setTime(getInputDate()); 

     gmtTime.clear(); 
     gmtTime.set(estTime.get(Calendar.YEAR), estTime.get(Calendar.MONTH), 
       estTime.get(Calendar.DAY_OF_MONTH), estTime.get(Calendar.HOUR_OF_DAY), estTime.get(Calendar.MINUTE)); 
     gmtTime.set(Calendar.SECOND, estTime.get(Calendar.SECOND)); 
     Date estDate = gmtTime.getTime(); 
     System.out.println(estDate + "(Actually EST)"); 
    } 

    private static Date getInputDate() { 
     Calendar instance = Calendar.getInstance(); 
     instance.clear(); 
     instance.set(2014, 3, 2, 9, 0, 0); 
     Date input = instance.getTime(); 
     return input; 
    } 

} 

(뉴욕의 시간대 될 수 있습니다)에 "UTC"(GMT)에서 시간을 변환하려고 목록을 아래에 확인하고 출력은

Wed Apr 02 09:00:00 IST 2014 (Actually GMT) 
Wed Apr 02 05:00:00 IST 2014(Actually EST) 

제발한다 correct

편집는 실제로 : 그 날의 빛을 저장

을 고려하는 대신 "EST"의 "미국/뉴욕"을 사용하는 것이 중요
관련 문제