시간대 및 일광 절약 시간은 악몽 : 인도는 예를 들어 GMT +5이다. 당신은 분명이 일을 직접해서는 안됩니다. Joda-Time이 무거운 짐을 들게하십시오.
는 Using Joda time to get UTC offset for a given date and timezone, 비슷한 질문에 this answer를 참조하십시오. DateTimeZone 클래스는 getOffset() 메소드를 제공합니다.
자바 7 Joda 타임 2.3
예제 소스 코드 ...
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");
org.joda.time.DateTime now = new org.joda.time.DateTime(californiaTimeZone);
int millisecondOffsetToAddToUtcToGetLocalTime = californiaTimeZone.getOffset(now);
System.out.println("millisecondOffsetToAddToUtcToGetLocalTime: " + millisecondOffsetToAddToUtcToGetLocalTime);
// Note the casting to doubles to avoid integer truncation. Time zone offsets are NOT always whole hours.
System.out.println("Offset in decimal hours: " + (double)millisecondOffsetToAddToUtcToGetLocalTime/1000d/60d/60d);
2013-11-20T01에서 실행하는 경우 : 03 : 56.464-08 : ... 00
millisecondOffsetToAddToUtcToGetLocalTime: -28800000
millisecondOffsetToAddToUtcToGetLocalTime in hours: -8.0
중요 해당 숫자 서식 -8.0
은 오프셋에 대해이 잘못되었습니다. 콜론과 두 자릿수 (영 선도 패딩)와
-08:00
:이어야합니다.
-08
앞에 0이옵니다.
당신은 오프셋 로컬 영역을 나타내는 시간에 int를 사용하면 안됩니다. 많은 장소에는 실제 30 분의 오프셋이 있습니다 (예 : 인도). 당신은 당신의 int로 그것을 표현할 수 없었습니다. joda 시간은 수 밀리 초를 나타내는 이유가 있습니다. – LordOfThePigs
그게 좋은 생각입니다. 나는 거의 잊어 버렸다. –