2011-10-18 4 views
1

다음과 같이 다른 시간대의 유효한 날짜 문자열을 UTC으로 변환했습니다.Joda-Time을 사용하여 다른 시간대의 UTC로 변환하는 방법

String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; 
DateTimeFormatter DATETIME_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT); 
DateTimeZone dateTimeZone = DateTimeZone.forID("-03:00"); 
//date is 2000-01-01 00:00:00 -03:00 
DateTime date = DATETIME_FORMATTER.withZone(dateTimeZone).parseDateTime("2000-01-01 00:00:00")); 
System.out.println("Current date is: " + date.toString()); 
//now convert to UTC 
DateTime convertedDate = date.toDateTime(DateTimeZone.UTC); 
System.out.println("Converted date: " + date.toString()); 

결과는

Current date is: 2000-01-01T00:00:00.000-03:00 
Converted date: 2000-01-01T03:00:00.000Z 

이 일의 짧은/더 나은 방법이 무엇입니까? 최종 날짜를 Joda-TimeDateTime 개체로 지정합니다.

답변

0

DateTime의 시간대는 withZone()을 사용하여 변환 할 수 있습니다. 입력 문자열에 시간대 오프셋이 지정되어 있지 않으면 코드를 추가해야하므로 코드가 상당히 최적화됩니다.

0

예제 코드를 개선하면 하드 코드 된 "-3 : 00"오프셋이 표준 시간대 이름으로 바뀔 수 있습니다. 그러면 Joda-Time에서 가능한 모든 조정을 할 수 있습니다. Daylight Saving Time (DST). doc을 DateTimeZone.forID()에 대해 확인하십시오.

이 :

대신의
DateTimeZone dateTimeZone = DateTimeZone.forID("America/Sao_Paulo"); 

:

DateTimeZone dateTimeZone = DateTimeZone.forID("-03:00"); 

시간대 목록 (아마도 오래된, 메모를 읽기) :
http://joda-time.sourceforge.net/timezones.html

관련 문제