2014-03-29 1 views
1

Date를 현재 TimeZone에서 UTC로 변환하고 싶습니다.현재 시간대에서 UTC 감소분을 1 대신 2 시간으로 변환합니다.

결과를 이해할 수 없습니다.

코드 :

public static String convertToUTC(String dateStr) throws ParseException 
{ 

    Log.i("myDateFunctions", "the input param is:"+dateStr); 

    String uTCDateStr; 


    Date pickedDate = stringToDate(dateStr, "yyyy-MM-dd HH:mm:ss"); 

    Log.i("myDateFunctions", "the input param after it is converted to Date:"+pickedDate); 


    TimeZone tz = TimeZone.getDefault(); 
    Date now = new Date(); 
    Log.i("myDateFunctions:", "my current Timezone:"+tz.getDisplayName()+" +"+(tz.getOffset(now.getTime())/3600000)); 

    // Convert to UTC 
    SimpleDateFormat converter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    converter.setTimeZone(TimeZone.getTimeZone("UTC")); 
    uTCDateStr = converter.format(pickedDate); 

    Log.i("myDateFunctions", "the output, after i converted to UTC timezone:"+uTCDateStr); 

    return uTCDateStr; 

} 

그리고 로그 캣 결과는 다음과 같습니다

03-29 20:31:46.804: I/myDateFunctions(18413): the input param is:2014-04-29 20:00:00 
03-29 20:31:47.005: I/myDateFunctions(18413): the input param after it is converted to Date:Tue Apr 29 20:00:00 CEST 2014 
03-29 20:31:47.005: I/myDateFunctions:(18413): my current Timezone:Central European Time +1 
03-29 20:31:47.005: I/myDateFunctions(18413): the output, after i converted to UTC timezone:2014-04-29 18:00:00 

당신이 볼 수 있듯이 : 내 시간대가

그런 CET (GMT + 1) 왜 내 경우 입력 : 20:00 내가 19:00 대신 18:00에 돌아 왔습니까?

+0

시간대에 일광 절약 시간제가 있습니까? 그렇다면 변환에 1 시간이 추가 될 수 있습니다 (UTC는 변환하지 않습니다). –

+0

나는 항상 stackoverflw에서 ppl이 답을주는 것을 두려워하는 것을 방황합니다. 아마 downvotes 때문에? 글쎄, 네 말이 맞아. 동의 할 수 있도록 답장으로 작성하십시오. –

+0

나는 추측을 할 때 의견을 사용하기 때문에 목록을 보는 사람들은 대답을 전제로하지 않습니다. –

답변

2

문제는 일광 절약 시간입니다. UTC에는 없지만, 일년 중 한 시간 동안 차이가 1 시간 증가합니다.

0

answer by Game Sechan이 정확합니다.

악명 높은 java.util.Date 및 .Calendar 클래스보다는 Joda-Time 또는 java.time을 사용할 때이 작업이 얼마나 쉬워 졌는지 보여주고 싶습니다.

Joda 타임 Joda-Time 2.4에서

.

String inputRaw = "2014-04-29 20:00:00"; 
String input = inputRaw.replace(" ", "T"); 
DateTimeZone timeZoneIntendedByString = DateTimeZone.forID("America/Montreal"); // Or DateTimeZone.getDefault(); 
DateTime dateTime = new DateTime(input, timeZoneIntendedByString); 
DateTime dateTimeUtc = dateTime.withZone(DateTimeZone.UTC); // Adjust time zones, but still same moment in history of the Universe. 
관련 문제