2012-04-23 3 views
1

DateTimeZone과 함께 DateTime의 시간을 확인하는 기본 제공 방법이 있습니까?Joda Time 일광 절약 시간 탐지

작은 유틸리티 클래스를 작성했지만 기본 제공 방법을 선호합니다.

public class Dst { 
    public static long dayHours(DateTime instant) { 
     Period day = new Period(0, 0, 0, 1, 0, 0, 0, 0); 
     return new Duration(instant, instant.plus(day)).getStandardHours(); 
    } 
    public static boolean hasClockChange(DateTime instant) { 
     return 24l != Dst.dayHours(instant); 
    } 
} 

답변

3
내가 거기 JodaTime 라이브러리에 직접적인 방법이지만, 어쩌면 몇 가지 짧은 생각하지 않습니다

:

public static long dayHours(DateTime instant) { 
    return new Duration(instant.withMillisOfDay(0), 
         instant.withMillisOfDay(0).plus(Days.ONE)) 
         .getStandardHours(); 
} 

보이는 때문에 하루에, 당신은 무엇을 게시 약간 diffrent이다는 표현 제공된 시간 (시간 무시)이 아닌 다음에 23/24/25 시간이 아닌 실제 시간은 instant에 포함 된 시간이 시간 변경 전후인지 여부에 따라 달라집니다.

내가 스스로에게 묻는 질문은 실제로 시간이 필요합니까? 아니면 그날에 변화가 있었는지 아닌지? 다음과 같이 사용하십시오.

public static boolean hasClockChange(DateTime instant) { 
     zone = DateTimeZone.forID("your-time-zone") // or do this with an appropriate constant 
     return zone.getOffset(instant.withMillisOfDay(0)) != zone.getOffset(instant.withHourOfDay(23)); 
} 

(시간 변경은 오후 11시 이후에는 발생하지 않습니다. 괜찮습니다). 정보가 필요한 경우 시간 변경 방향에 따라 -1/0/1을 반환 할 수도 있습니다.