1. 준비 지식
1. 미국의 일광 절약 시간/new_york는 다음과 같습니다. 왼쪽은 오른쪽이 아닙니다.
2016-3-13, 02:00:00 ~ 2016-11-6, 02:00:00
2017-3-12, 02:00:00 ~ 2017-11-5, 02:00:00
2. 3 글자 시간대 ID
JDK 1.1.x와의 호환성을 위해, 약 3 글자 시간대 ID (예 : "PST", "CTT", "AST")도 지원됩니다.
그러나 동일한 약어가 종종 여러 시간 영역에서 사용되므로 사용이 폐기됩니다.
예를 들어, CST : 미국, 호주, 중국, 쿠바 시간의 4 가지 의미가 있습니다.
3. 표준
GMT : Green Mean Time Greenwich Standard Time은 표준 시간 참조 GMT+12-> GMT-12 이전으로 사용되었습니다.
Java8의 범위는 GMT+18-> GMT-18입니다
UTC : 조정 된 범용 시간 시간은 GMT보다 정확한 세계 시간을 조정하며 1972 년 1 월 1 일에 새로운 표준이됩니다. UTC, UTC+1, UTC+2 ... UTC+12, UTC-12 ... UTC-1
Java8의 범위 UTC-18-> UTC+18
DST : Daylight Saving Time은 여름에 시계가 한 시간 동안 일찍 햇빛을 사용하여 일찍 햇빛을 사용하는 시간을 나타냅니다. 영국에서 여름 시간이라고합니다.
현재 110 개국 이상이 일광 절약 시간을 사용합니다.
중국에서는 1986 년부터 1992 년까지 6 년 동안 만 시행되었으며 그 후 취소되었습니다. 이유는 다음과 같습니다.
1. 중국은 동서 방향에 큰 스팬을 가지고 있으며, 일광 절약 시간을 사용하여 동쪽과 서쪽과 양립 할 수없는 통일 된 동쪽 8 학군을 채택합니다.
2. 고등 지역에서는 겨울과 여름에 낮과 밤의 시간이 크게 바뀝니다. 그다지 의미가 없습니다.
4. 동쪽 8 학군이 GMT+8 또는 etc/gmt-8 (다른 방법으로, 왜 그런가?
5. 중국의 시간대를 대표하는 방법
GMT+8
UTC+8
아시아/하빈 하브 린 // 중국 표준 시대
아시아/충칭 충칭 // 중국 표준시
아시아/Chungking Chongqing // China Standard Time
아시아/urumqi Urumqi // 중국 표준 시간
아시아/상하이 상하이 (East 8 District) // China Standard Time
PRC
아시아/마카오 마카오 // 중국 표준 시간
홍콩 홍콩 // 홍콩 시간은 중국 표준 시간과 일치합니다.
아시아/홍콩 홍콩
아시아/타이페이 타이페이 (대만) // China Standard Time 싱가포르는 중국의 시대와 동일합니다.
아시아/싱가포르
싱가포르
6. 표준 시간 영역의 표현
UTC
UTC+0
UTC-0
GMT GMT
GMT0 GMT
etc/gmt gmt
etc/gmt+0 gmt
etc/gmt-0 gmt
ETC/GMT0 GMT0 GMTS 표준 시간 참고 : GMT+XX (-XX)는 매우 포괄적이며 다양한 시간의 표현을 자동으로 식별 할 수 있습니다.
2. 시간대 변환
환경 : Java8 이전
1. 현재 시간을 지정된 시간대 디스플레이로 변환합니다.
@test public void test ()는 예외를 던져 {date a = new Date (); simpledateformat sf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); sf.settimezone (timezone.gettimezone ( "America/New_York")); // 중국 시간 구역을 미국 시스템의 뉴욕 시간대로 변환합니다. out.println (sf.format (a)); }2. 지정된 시간이 지정된 시간대 디스플레이로 변환됩니다.
제대로 올바르게 변환 할 수 있습니까? 함정이있는 것 같습니다. 온라인 구현을 보았습니다.
나는 일광 절약 시간에 약간의 문제를 느낍니다.
// 구현 방법 1 : 일광 절약 시간으로 간주되지 않습니다 @Test public void test2 () 예외 { /date datetime = new SimpledateFormat ( "yyyy-mm-dd hh : mm : ss"). 구문 분석 ( "2016-11-6 14:00:00"); DATE DATETIME = NEW SMEPLEDATEFORMAT ( "YYYY-MM-DD HH : MM : SS"). 구문 분석 ( "2016-10-6 14:00:00"); TimeZone Zhong = TimeZone.getTimezone ( "GMT+8 : 00"); // China TimeZone York = Timezone.getTimezone ( "America/New_York"); // gmt-5 // 여기에 시간대 오프셋이 고정되어 있습니다. 일광 절약 시간, 오류 long chinesemills = datetime.gettime () + kork.getRawoffset ()-zhong.getRawoffset (); 날짜 = 새 날짜 (Chinesemills); System.out.println (New SimpledateFormat ( "yyyy-mm-dd hh : mm : ss"). 형식 (날짜)); } // 구현 방법 2 calendar.dst_offset calendar class의 calendar.dst_offset을 사용하여 Calendar.dst_offset이 여전히 @testpublic void test3 ()가 예외를 던지지 않는다는 것을 기억할 수 있습니다. = New SimpledateFormat ( "YYYY-MM-DD HH : MM : SS"). PARSE ( "2016-11-6 1:00:00"); // 날짜 시간 = New SimpledateFormat ( "YYYY-MM-DD HH : MM : SS"). PARSE ( "2016-11-6 1:00:00:00:00 : // DATADEFORAT ("yyyy-MM-DD HH : MM : SS "); HH : MM : SS "). 구문 분석 ("2016-11-6 0:59:00 "); // 날짜 시간 = 새로운 simpledateformat ("yyyy-mm-dd hh : mm : mm : ss "). 구문 분석 ("2016-11-6 1:59:59 "); 날짜 시간 = 새로운 단순한 simpledateformat ("yyyy-mm-ddd HH : MM : SS "). 구문 분석 ("2016-11-6 3:00:00 "); // 1. 현지 시간을 얻으십시오 : Calendar Cal = Calendar.getInstance (); Cal.setTime (Time); Cal.settimezone (Timezone.getTimezone ("America/New_York "); // 2. 시간이 고정되어 있습니다. cal.get (calendar.zone_offset)/(1000*60*60); // 3. 일광 절약 시간 차이를 얻으십시오 : 이것은 시간의 동적 판단에 따라 고정되어 있습니다. 시간대에 일광 절약 시간이있는 한, 그것은 1int dstoffset = cal.get (calendar.dst_offset)/(1000*60*60); System.out.println (ZoneOffset); system.out.println (dstoffset); // 4. UTC 시간을 얻기 위해 이러한 차이를 공제합니다. -(ZoneOffset + dstoffset)); cal.add (calendar.hour, -(ZoneOffset + dstoffset)); date time2 = cal.gettime (); system.out.println ( "yyyy -mm -dd hh : mm : ss"). 형식 (time2)); // 구현 방법 3 준비 // Java가 우리를 위해 일광 절약 시간을 자동으로 처리한다고 말하지 않습니까? // 일광 절약 시간 절약 방법이 올바른지 확인하기 위해 간단한 테스트를하자, Java의 자동 처리가 정확한지 여부를 추론 할 수 있습니다. // 2016 년 : America/New_York 's Daylight Saving Saving Time Is : 2016-3-13 02:00:00 ~ 2016-11-06 01:59:59 Public void Test4 () @) and time reation at at at at a time antop and time a time a time a time at and time a Time Ports. simpledateformat sf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); sf.settimezone (timezone.gettimezone ( "gmt+0")); // date datetime = sf.parse ( "2016-11-6 5:59:59"); 날짜 d1 = sf.parse ( "2016-03-13 6:59:59"); // false date d2 = sf.parse ( "2016-03-13 7:00:00"); // true d3 = sf.parse ( "2016-11-6 6:59:59"); // false d4 = sf.parse ( "2016-11-6 7 : 00:00:00 : // 일광 절약 시간의 시작을 판단하는 데 실제로 아무런 문제가 없다는 것을 알았지 만, 일광 절약 종료 시간이 끝날 때 정확히 한 시간 전에 판단했습니다. 그것을 해결하는 방법? 현재 Java8 D3 = sf.parse 만 사용할 수 있습니다 ( "2016-11-6 5:59:59"); // true d4 = sf.parse ( "2016-11-6 6:00:00"); // false timezone York = timezone.gettimezone ( "America/New_York"); // gmt-5 system.out.println ( "대상 시간대는 일광 절약 시간을 사용합니까?"+isdaylight (York, D1)); System.out.println ( "대상 시간대는 일광 절약 시간을 사용합니까?"+isdaylight (York, D2)); System.out.println ( "대상 시간대는 일광 절약 시간을 사용합니까?"+isdaylight (York, D3)); System.out.println ( "대상 시간대는 일광 절약 시간을 사용합니까?"+isdaylight (York, D4)); } // Private Boolean isdaylight (TimeZone Zone, Date Date) {return Zone.usedAylightTime () && Zone.IndayLightTime (날짜); } // 구현 방법 3 // 위의 확인을 통해 시스템의 판단이 문제가 있음을 알고 있습니다. 시간대를 설정함으로써 프로그램은 자동으로 일광 절약 시간을 처리합니다. 적어도 @testpublic void test5 ()가 예외를 던지는 이유를 이해할 수 없습니다 {// 13 시간 중국 + 8 New York-5 ChangeZone ( "2016-3-13 14:59:59", "PRC", "America/New_York", "yyyy-mm-dd hH : mm : ss"); //03-13 01:59-3-3-13. 15:00:00 ","PRC ","America/New_York ","Yyyy-MM-DD HH : MM : SS "); // 2016-03-13 03:00:00 ChangeZone ("2016-11-6 13:59:59 ","PRC ","America/New_York ","Yyyy-MM-DD HH : MM : SS "); 01:59:59 //이 결과는 틀렸다. 02:00:00 ChangeZone ( "2016-11-6 14:00:00", "PRC", "America/New_York", "YYYY-MM-DD HH : MM : SS"); // 2016-11-06 01:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00}; Time, String Srcid, String Destid, String Pattern) ParseException 던지기 {// 기본 시간대 시간대 구역 = Timezone.getTimezone (srcid); TimeZone.SetDefault (Zone); 날짜 날짜 = 새로운 simpledateformat (패턴) .parse (시간); // 대상 시간대 시간대 시간대 시간대 DESTZONE = TIMEZONE.GETTIMEZONE (destid); simpledateformat sdf = 새로운 simpledateformat (패턴); // 시간대가 서식으로 설정된 sdf.settimezone (destzone); 문자열 창틀 = sdf.format (날짜); // 대상 시간대 시스템을 가져옵니다. out.println ( "시간대 수정 후" + destzone.getId () + "time :" + Changtime); }요약 : 위의 세 가지 구현 방법에 의해 얻은 결과는 일광 절약 시간에 다소 문제가됩니다.
3, Java8 구현
1. Java 8의 시간대 변경을 살펴 보겠습니다.
// jdk8의 사용 가능한 시간대 @TestPublic void testname1 ()은 예외를 발생시킵니다 {// jdk8의 모든 시간 영역 세트 <string> ids = zetavailablezoneids (); string [] id1 = ids.toArray (new String [ids.size ()]; string idss = arrays.tostring ( "). ",]); system.out.println (ids.size ()); // 28 JDK8의 문자열 [] id2 = timezone.getavailableIds (); system.out.println (id2.length); // 623 // for (string id : id2) {if (! idss.contains (id+",") {system.out.print (id+",");}} // 결론 : JDK8의 모든 시간 영역이 다음과 같이 // jdk8에 모두 삭제되었습니다. Agt, Art, Ast, Bet, BST, Cat, Cnt, CST, CTT, Eat, Ect, IET, IST, JST, MIT, NST, NST, PLT, PNT, PRT, PST, SST, VST, //이 짧은 이름은 실제로 사용할 수 없으며 지원 목록에서 사용할 수 없습니다. localDateTime.ofinstant (instant.now (), ZoneId.of (ZoneId.short_ids.get ( "pst"))); System.out.println ( "/ndate =" + date);}2. 시간대 정보를 추가하거나 변환하는 방법
// LocalDate, LocalDateTime, Instant 추가 시간대 정보 @TestPublic void testname3 () {localDate date = localDate.now (); // localDate 추가 시간대 정보 추가 // 메소드 1zonedDateTime Zone = date.atstartOfday ( "GMT+08 : 00"); System.out.out.out.out.out.out.out.out.out.out.out.out. date.atstartofday (ZoneId.SystemDefault ()); System.out.println (Zone); // 방법 2 LocalDate Date2 = LocalDate.now(ZoneId.of("GMT+0"));System.out.println(date2);System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Zoneime3 = localDateTime.now (clock.system ( "gmt+0")); // 메소드 4zoneddateMe 4 = ZonedDateTime.of ( "gmt+0"); system.out.out.out.println (ZoneTime); System.out.println (Zonetime2); // system.out.println (Zonetime3); // system.out.println (Zonetime4); // Change System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.3. 현재 시간의 지정된 시간을 얻는 방법 (테스트 생략)
// 현재 시간의 지정된 시간대를 가져옵니다. 참조 점 : 0 Time Zone Public LocalDateTime getCurrentZoneTime (ZoneId dest) {Objects.RequirenOnnull (dest); LocalDateTime Time2 = localDateTime.now (Clock.System (dest)); String Zonedesc = getzonedesc (Timezone.getTimezone (dest)); System.out.println (dest.getId ()+"는 표준 시간대에 해당합니다 :"+Zonedesc); System.out.println ( "대상 시간대"+dest+"time"+time2.format (datetimeformatter.ofpattern ( "yyyy-mm-dd hh : mm : ss")); return time2;} // 표준 시간대, 메소드 1 // JDK8 이전의 메소드를 가져 오십시오. 시간대 개인 정적 문자열 getzonedesc (TimeZone Destzone) {objects.requirenonnull (destzone); int offset = destzone.getRawoffset () / (1000 * 60 * 60); if (오프셋 <= 0) {return "gmt"+string.valueof (오프셋); } else {return "gmt +" + string.valueof (오프셋); }} // java8 method, method 2, zonerules 사용 // 시간대의 표준 오프셋을 가져옵니다. rule.getStandardOffset (ZonedDateTime.now (dest) .toinstant ()). getId (); 문자열 s = StandardOffset.split ( ":") [0]; int 오프셋 = integer.parseint (s); // 반환 메소드 1 : 몇 시간 및 분으로 // "gmt"+StandardOffset을 반환합니다. // 반환 메소드 2 : (오프셋> 0) {return "gmt+"+orpset;} else {return "gmt"+offset;}}4. 지정된 시간대의 지정된 시간을 얻는 방법
위에 남은 시간대 문제를 구현하기 시작하십시오
마찬가지로 먼저 Java8을 사용하는 일광 절약 시간 판단 방법이 올바른지 확인해 봅시다.
// 미국의 시간이 일광에 있는지 여부를 결정하기 위해 직접 작성하십시오. 공개 부울 isdaylighttime (localDateTime a) {Objects.Requirenonnull (a); LocalDateTime startDate = A.WithMonth (3) .tolocaldate (). Attime (2, 0); LocalDateTime StartlightDay = StartDate.with (tempalAdjusters.dayofweekinmonth (2, dayofweek.sunday)); // 11 월 LocalDateTime EndDate = A.WithMonth (11) .ToloCaldate (). ATTIME (1, 59,59); LOCALDATETIME ENDLIGHTDAY = endDate.with (tempalAdjusters.dayofweekinmonth (1, dayofweek.sunday)); if (a.isbefore (startlightday) || a.isafter (endlightday)) {system.out.println ( "일광 절약 시간이 아님+a);} system.out.println ("+a in in in system. java8 이미 기성품 방법이 있습니다.이 방법은 이미 지정된 시간과 시간대 공개 부울 isdaylighttime (LocalDateTime a, ZoneId dest) {ZonedDateTime Z1 = A.ATZONE (dest); // 또는이 방식으로 전송 // ZONEDDATETIME Z2 = ZONEDDATETIME.OF (a, dest); System.out.println (z1.format (dateTimeformatter.ofPattern ( "yyyy-mm-dd hh : mm : ss")); ZONERULES 규칙 = dest.getRules (); 부울 플래그 = rular.isdaylightsavings (z1.toinstant ()); System.out.println (플래그); 리턴 플래그; }// 테스트를 통해 java8의 일광 절약 시간 방법이 완전히 정확하다는 것을 알았습니다.
// 2016 년 : America/New_York 's Daylight Saving Saving Time은 다음과 같습니다. 2016-3-13 02:00:00 ~ 2016-11-06 01:59:59 // (3 월 두 번째 일요일과 11 월 첫 번째 일요일) @TestPublic void testName () @Testpublic void testName () 예외 {// localdatetime a1 = localDateTime.now (); LocalDateTime a2 = LocalDateTime.of (2016, 3, 13, 1, 59,59); LocalDateTime A3 = LocalDateMe.of (2016, 3, 13, 2, 00); LocalDateTime A4 = LocalDateTime.of (2016, 11, 6, 1, 59,59); LocalDateTime A5 = LocalDateMe.of (2016, 11, 6, 2, 0,0); // ISDAYLIGHTTIME (A2); // ISDAYLIGHTTIME (A3); // ISDAYLIGHTTIME (A4); // ISDAYLIGHTTIME (A5); SYSTEM.OUT.PRINTLN ( "============================= ===================================================================================== ===================================================================================== ===================================================================================== ===================================================================================== ===================================================================================== ===================================================================================== ); // falseisdaylighttime (a3, Zoneid.of ( "America/New_York")); // trueisdaylighttime (a4, Zoneid.of ( "America/New_York")); // trueisdaylighttime (a5, Zoneid.of ( "America/New_York")); // fasle}구현 시작 :
Version 1:
// 지정된 시간에 지정된 시간 참조 점을 가져옵니다. 기본 시간대 공개 LocalDateTime getzongtime (localDateTime Time, ZoneId dest) {objects.RequirenOnnull (dest); getzongtime을 반환합니다 (시간, null, dest); } // 시간대를 지정하면 여기에 시간대가 지정되면이 시간대 공개 LocalDateTime getzongtime (LocalDateTime Time, Zoneid SRC, ZoneId Dest) {// 여기에서의 어려움은 중국이며 미국은 미국입니다. 이것은 아마도 물체에 더 적합 할 것입니다 .requirenonnull (dest); ZONEDDATETIME Z1 = NULL; if (src == null) {z1 = time.atzone (ZoneId.systemDefault ()); } else {z1 = time.atzone (src); } // 시간 구역 응답 시간 ZonedDateTime z2 = z1.withzonesameinstant (dest); System.out.println (dest.getid ()+"는 표준 시간대에 해당합니다. System.out.println ( "대상 시간대"+dest+"time"+z2.format (datetimeformatter.ofpattern ( "yyyy-mm-dd hh : mm : ss")); System.out.println ( "----------------"); 반환 시간; }테스트는 다음과 같습니다.
@test public void test6 ()는 예외를 겪고 {// 일광 절약 시간 절약 시간 2016-03-13 01:59:59 LocalDateTime Time4 = localDateTime.of (2016, 3, 13, 14, 59, 59); getzongtime (Time4, Zoneid.of ( "America/New_York")); // 일광 절약 시간 절약 시간 2016-03-13 03:00:00 LocalDateTime Time1 = localDateTime.of (2016, 3, 13, 15, 00, 00); getzongtime (Time1, Zoneid.of ( "America/New_York")); // 일광 절약 시간 절약 시간 : 2016-11-06 01:59:59 // 다시 실패한 것 같은 느낌이 든다. 다시 말해서 Java8이 일광 절약 시간을 처리하기 전의 방법은 정확히 동일하며,이 시간의 시간 절약 시간은 2016-1-1-6 일의 종말 시간을 판단합니다. 00:59:59이지만 Java8의 판단 방법이 정확합니다. 약간 이상한 LocalDateTime Time2 = LocalDateTime.of (2016, 11, 6, 14, 59, 59); getzongtime (Time2, Zoneid.of ( "America/New_York")); // 일광 절약 시간 절약 2016-11-06 02:00:00 LocalDateTime Time3 = LocalDateTime.of (2016, 11, 6, 15, 00, 00); getzongtime (Time3, Zoneid.of ( "America/New_York")); }그래서 이제이 결과가 체계적인 계산 문제인지 의심 스럽거나 뉴욕 관세를 이해하지 못하는가?
그러나 여전히 원하는 결과를 얻을 수 있습니다. 두 가지 방법을 사용합니다.
withearLierOffSetAtOverLap (), withLaterOffSetAtOverLap ()
Version 2:
// 지정된 시간에 대한 지정된 시간 기준점을 가져옵니다 : 기본 시간대 공개 LocalDateTime getzongtime2 (LocalDateTime Time, Zoneid dest) {Objects.Requirenonnull (dest); return getzongtime2 (시간, null, dest);} // Public LocalDateMe Getzont2 (localDateTime Time, Zoneid src, fin 오프셋 // 기본 시간대는 중국에서 미국에서 미국이 미국이므로 대상에 더 적합 할 것입니다. requirenonnull (dest); ZONEDDATETIME Z1 = NULL; if (src == null) {z1 = time.atzone (ZoneId.systemDefault ()); } else {z1 = time.atzone (src); } // ZONEDDATETIME Z2 = Z1.withzonesameInstant (dest); // 중첩 문제 핸들 긴 시간 = 시간 간격 (Z2.WitHearLierOffSetAtOverLap (), Z2.WithLaterOffSetAtOverLap ()). ToHours (); z2 = z2.plushours (시간); System.out.println (dest.getid ()+"는 표준 시간대에 해당합니다. System.out.println ( "대상 시간대"+dest+"time"+z2.format (datetimeformatter.ofpattern ( "yyyy-mm-dd hh : mm : ss")); System.out.println ( "------------------"); 반환 시간;}테스트 : OK
@test public void test4 ()는 예외를 던졌습니다. {// 일광 절약 시간 절약 시간 2016-03-13 01:59:59 LocalDatetime Time4 = localDateTime.of (2016, 3, 13, 14, 59, 59); getzongtime2 (Time4, Zoneid.of ( "America/New_York")); // 일광 절약 시간 절약 시간 2016-03-13 03:00:00 LocalDateTime Time1 = localDateTime.of (2016, 3, 13, 15, 00, 00); getzongtime2 (Time1, Zoneid.of ( "America/New_York")); // 일광 절약 시간 2016-11-06 02:59:59 LOCALDATETIME TIME2 = localDateTime.of (2016, 11, 6, 14, 59, 59); getzongtime2 (Time2, Zoneid.of ( "America/New_York")); // 일광 절약 시간에 예상되지 않습니다. 2016-11-06 02:00:00 LocalDateTime Time3 = LocalDateTime.of (2016, 11, 6, 15, 00, 00); getzongtime2 (Time3, Zoneid.of ( "America/New_York")); }결과:
America/New_york는 표준 시간대 : GMT-5에 해당합니다
대상 시간대 미국/New_York 2016-03-13 01:59:59
------------------------
America/New_york는 표준 시간대 : GMT-5에 해당합니다
대상 시간대 미국/New_York 2016-03-13 03:00:00
------------------------
America/New_york는 표준 시간대 : GMT-5에 해당합니다
대상 시간대 미국/New_York 2016-11-06 02:59:59
------------------------
America/New_york는 표준 시간대 : GMT-5에 해당합니다
대상 시간대 미국/New_York 2016-11-06 02:00:00의 시간
------------------------
Java Time Zone을 기반으로 일광 절약 시간을 변환하는 위의 문제와 해결책은 내가 공유하는 모든 콘텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.