2014-04-16 3 views
2

Java에서 yoda 시간 라이브러리를 사용하여 UTC 시간을 CET 시간으로 변환하려면 어떻게합니까?UTC 시간을 Java에서 CET 시간으로 변환하는 방법

나는이 위트 triing했다하지만 난에 넣어 나는 같은 시간을 받기를 사용하면 내가

DateTime dateTime = new LocalDateTime(utdDate.getTime()).toDateTime(DateTimeZone.forID("CET")); 

뭔가를 잘못하고 있어요 것 같습니다.

+0

http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some- : 당신이 필드를 유지하려는함으로써 인스턴트을 변경하는 경우에 당신은 방법 withZoneRetainFields을 사용할 수 있습니다 other-timezone-in-java – Jay

답변

1

난 강력하게 당신을 조언 고유 한 지역화 된 특성으로 인해 "CET"와 같은 시간대 이름을 사용하지 마십시오. 이것은 최종 사용자의 형식화 된 출력에서만 유효하지만 내부 코딩에서는 그렇지 않을 수 있습니다.

CET 내 시간대 "유럽/베를린"에서 IANA-ID를 Europe/Berlin, Europe/Paris 등 같은 많은 다른 시간대 식별자를 의미 코드는 다음과 같이 작동

DateTime dateTime = 
    new LocalDateTime(utdDate.getTime()) // attention: implicit timezone conversion 
    .toDateTime(DateTimeZone.forID("CET")); 
System.out.println(dateTime.getZone()); // CET 
System.out.println(dateTime); // 2014-04-16T18:39:06.976+02:00 

는 기억 표현이 new LocalDateTime(utdDate.getTime())implicitly uses the system timezone for conversion과 의지 따라서 CET 영역이 시스템 시간대와 비교하여 동일한 시간대 오프셋으로 내부적으로 인식되는 경우 아무 것도 변경하지 마십시오. UTC-입력을 인식 할 수 JodaTime을 강제하기 위해이 방법처럼 지정해야합니다 :

Date utdDate = new Date(); 
DateTime dateTime = new DateTime(utdDate, DateTimeZone.UTC); 
System.out.println(dateTime); // 2014-04-16T16:51:31.195Z 

dateTime = dateTime.withZone(DateTimeZone.forID("Europe/Berlin")); 
System.out.println(dateTime); // 2014-04-16T18:51:31.195+02:00 

이 예는 UNIX의 시대 이후 밀리 초 단위 절대 시간이 순간을 유지합니다.

Date utdDate = new Date(); 
dateTime = new DateTime(utdDate, DateTimeZone.UTC); 
System.out.println(dateTime); // 2014-04-16T16:49:08.394Z 

dateTime = dateTime.withZoneRetainFields(DateTimeZone.forID("Europe/Berlin")); 
System.out.println(dateTime); // 2014-04-16T16:49:08.394+02:00 
관련 문제