ãã®èšäºã¯ãæ·±ã¿ãããããŸãã¯åŠè¡çã§ãããšäž»åŒµããŠããŸããã ããã¯ãçµéšãäœç³»åããéçºè ã®æ³šæãããŸãæçœã§ãªãåŽé¢ã«åããããšããè©Šã¿ã§ãã
æ°ããDate Time APIã JDK8ã«ç»å ŽããŸããããã®APIã«ã¯ ãå€ãã®æ°ãã䟿å©ãªã¯ã©ã¹ãç»å ŽããŸããã ã åªãããµãŒãããŒãã£ã©ã€ãã©ãªã«ã€ããŠã¯èšåããŸããã ããã¯ãå¥ã®èšäºã«å€ããå¥ã®å€§ããªãããã¯ã§ãã ããã«ãæ¢ã«è¿°ã¹ãããã«ãç¹å®ã®æè¡ã«ã€ããŠã§ã¯ãªããå šäœãšããŠã®äœæ¥ã®ååã«ã€ããŠã話ãããããšããŠããŸãããã®ç¹ã§ãæ°ããAPIã¯åºæ¬çã«å€ããã®ãšå€ãããŸããã
æé軞
é ãããå§ããŸãããã ãšãŠãé ãã æé軞ãæããŸãã
ããããããŸããŸãªè³ªåãå§ãŸããŸãã æéã¯åžžã«åãæ¹åã«é²ã¿ãŸããïŒ åçã«è¡ããŸããïŒ é£ç¶ã§ããïŒ åäœãã¯ãã«ãšããŠäœãåããŸããïŒ ãã®è»žã¯ãã¹ãŠåãã§ããïŒ ç©ºéå ã®äœçœ®ã«äŸåããŸããïŒ åãã®éãããïŒ æå°æž¬å®é·ã¯äœã§ããïŒ
å®éãããã§ç§ã¯è³ªåãããæºåãã§ããŠããã ãã§ã決ããŠçããŸããã æéããªããšä¿¡ããããŠããŸãã ãç§ã¯ãã®åé¡ã«ã€ããŠè°è«ããæºåãã§ããŠããŸããã
ãããããã§ã«è§£æ±ºããããã€ã³ãããããŸãã
枬å®åäœã«ã€ããŠã¯ãã¹ãŠæããã§ããæ確ãã€æ確ã«æå®ãããŠããŸãã
ã¢ã¹ã¯ã¯ããã¯ã·ã³ãã³ãŸã§ã®è·é¢ã¯çŽ7840000ã¡ãŒãã«ã§ãå ã¯ãã®è·é¢ãå°ãªããšã0.026ç§ã§å°è¡šäžã移åããŸããããã¯éåžžã«é·ãæéã§ãã ãŠã©ãžãªã¹ãã¯ã§ãŠãŒã¶ãŒã¢ã«ãŠã³ããäœæãããªã¯ãšã¹ãã¯ããã°ããããŠããã¢ã¹ã¯ã¯ãµãŒããŒã§åŠçãããŸãã ãããã£ãŠãçºçããã€ãã³ãã«é¢ããæ å ±ã¯ããã«ã¯å©çšã§ããã空éå ã®ãã€ã³ãéã®è·é¢ã«äŸåããŸãã
ããã«ã GPSã®ãããªéåžžã«æ®éã®è¿å°çãã¯ãããžãŒã§ããæéã®é床èªäœã¯ãªããžã§ã¯ãã®é床ã«äŸåããŸãã
çŸåšã®æšæºJavaæéåŠçã©ã€ãã©ãªã¯ãçžå¯Ÿè«çå¹æããªãã誰ããå éã«è¿ãé床ã§åãããšã¯ãªããšèããŠãããæé軞ã¯ãã¹ãŠïŒå°ãªããšãåãææã§ïŒåãã§ãããç§ãã¡å šå¡ã«é©ããŠããŸãã ããããåŸã§ãæ°ããJava Date Time APIãJDKïŒ6543ã«å®è£ ãããŸããããã«ããã Millennium Falconå éšãªãã£ã¹ã·ã¹ãã çšã®ãµãŒãã¹ãäœæã§ãããã®é床ãšè¿ãã®ã¢ã«ããŒã«ã®æç¡ãèæ ®ãããŸãã
ããã§ãæé軞äžã®ç¹å®ã®ç¬éãããŒã¯ããŸãã ããšãã°ãä»ã¯ããããã¿ã³ãã¯ãªãã¯ããŸãã ïŒã¯ãªãã¯ïŒ
次ã«ããã®ãã¿ã³ãæŒããæ£ç¢ºãªã¿ã€ãã³ã°ã説æã§ããæ¹æ³ãèãåºãå¿ èŠããããŸãã ãããè¡ãæãç°¡åãªæ¹æ³ã¯ãç§ãã¡å šå¡ã«å ±éã®æç¹ãæå®ããããšã§ãããã®æç¹ãããåžžã«æéãµã³ãã«ãã«ãŠã³ãããŸãã ãã®ç¬éã瀺ãããŠããå ŽåïŒ ãã®åãç¬é ïŒããã®äžè¬çãªç¬éããã®ãµã³ãã«ã®æ°ãéä¿¡ããããšãã§ããŸãããã¿ã³ãæŒãããæéãšãå€ãååŸãããšãã®çŸåšã®æéãšã®é¢ä¿ãç解ã§ããŸãã
åå§çãªç©çäžçã§ã¯ãåãç æèšã«åºäŒã£ãŠåæã«èµ°ãããšãã§ããŸããã ãã®åŸãèœã¡çããŠããªãã®ããžãã¹ã調ã¹ãã€ãã³ãã®æéããŠã©ããã€ã³ã¹ã¿ã³ã¹ã®ç æ±ã®é«ãã®åœ¢ã§å ±åããŸãïŒããããããŠã©ããã¯éåžžã«å€§ãããŠããã°ãã¯ãã§ãïŒã
ç§ãã¡ã䜿çšããåãã¢ãŒã¡ã³ã㯠ã次å ã§ãããããŸãããããäžè¬çã§éèŠãªã€ãã³ãã«é¢ããŠã§ãã ç§ãã¡ã®å Žåãããã¯Javaã§äœ¿çšãããUnixã¿ã€ã ã·ã¹ãã ïŒæ°å€0ïŒã§åãç¬éã«èµ·ãããŸã-ããã¯ã1970幎1æ1æ¥ã®R.Khãã00:00:00ãšã©ãã«ä»ããããæç¹ã§ãã UTCã¯ãã§ã«å¥ã®ã¹ã±ãŒã«- ã°ã¬ãŽãªãªæŠã§ãã
Javaã¯ãããšäœã®é¢ä¿ãããã®ã
Javaã®æé軞ã«æå»ãèšå®ããããã«ãã¯ã©ã¹java.util.DateããããŸãã äžè¬ã«ãjava.util.Dateã¯ãæãåæã®ããŒãžã§ã³ããã®Javaã®æ¥ã§ãã ãŸãã圌ã®ååã¯æ¬è³ªãåæ ããŠããŸããã ãããŠç¬¬äºã«ã圌ã¯å¯å€ã§ãã ãã ãã ãã®åãç¬éããã®ããªç§æ°ãæ ŒçŽããlongåã®å éšãã£ãŒã«ãã®åçŽãªã©ãããŒãšããŠèªèãããšã人çã¯ããåçŽã«ãªããŸã-ãã以äžã¯äœããããŸããã ä»ã®ãã¹ãŠã®ã¡ãœããã¯å»æ¢ããããã®ãšããŠããŒã¯ãããŠãããã©ã®ãããªå Žåã§ããããã䜿çšããå¿ èŠã¯ãããŸããã java.utl.Dateã¯ïŒæ¬è³ªçã«ïŒããªç§åäœã®åçŽãªé·ãUnixæéã®æ°å€ãšåäžã§ããããšãèŠããŠãããŠãã ããã
Date moment = new Date(1451665447567L); // Unix-time -- moment.getTime(); // Unix-time --.
å°ãèããŠã¿ããšãã©ã®èšèªã§ãæéè¡šçŸã®ç²ŸåºŠã«å¶éãããããšãæããã«ãªããŸãã ãããã£ãŠãjava.util.Dateã¯ïŒä»ã®é¡äŒŒã¿ã€ããšåæ§ã«ïŒæé軞äžã®ãã€ã³ãã§ã¯ãªããã»ã°ã¡ã³ãã§ãã ç§ãã¡ã®å Žåãããªç§ã®æç¶æéã®ã»ã°ã¡ã³ãã ããããå®çšçãªèŠ³ç¹ããã¯ããã®ãããªç²ŸåºŠã¯ç§ãã¡ã«é©ããŠãããããããããã€ã³ããšåŒã³ç¶ããŸãã
Javaã®è¡šçŸã¯æåãã64ããããªã®ã§ã確ãã«ååãªãã®ããããŸãïŒ
ç§ãã¡ã«åå
Date theEnd = new Date(Long.MAX_VALUE); DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String text = dateFormat.format(theEnd); System.out.println(text); # August 17, 292278994 7:12:55 AM UTC
åã ã®ã«ã¬ã³ããŒãã£ãŒã«ãïŒå¹Žãæãæ¥ãæéãåãç§ãªã©ïŒã®èªã¿åã/èšå®/å€æŽãªã©ãæéã«é¢ããããŸããŸãªæäœã«ã¯ãã¯ã©ã¹java.util.CalendarããããŸãã ãŸãã眪ããªãããã§ã¯ãããŸãã-æäœäžãæã¯0ããå§ãŸãïŒå®æ°ã䜿çšããæ¹ãè¯ãïŒãæ¥ã¯1ããå§ãŸãããšã«æ³šæããŠãã ããã
java.util.Calendar
@Test public void testSunday() throws Exception { Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2016, Calendar.JANUARY, 5, 12, 30, 0); calendar.add(Calendar.DAY_OF_YEAR, -2); Assert.assertEquals(Calendar.SUNDAY, calendar.get(Calendar.DAY_OF_WEEK)); }
java.util.Calendarã®ããäžã€ã®æ¿ã£ãç¬éã¯ããã®äžã«å®å šãªæ¥ä»ïŒyyyyãMMãddãHHãmmãssïŒãèšå®ãããšãããªç§ã®æ°ã¯0ã«ãªã»ããããããåã®èšå®ãããç¬éããã®ããªç§ã®æ°ã«çãããŸãŸã§ãïŒã«ã¬ã³ããŒãå€æŽãããŠããªãå Žåã¯çŸåšã®æå»ïŒã ãããã£ãŠãã¿ã¹ã¯ã®æ¡ä»¶ã«åŸã£ãŠããªç§åäœã§0ãå¿ èŠãªå Žåããã®ãã£ãŒã«ãã¯ãã1åè¿œå ã®åŒã³åºãã§ãªã»ããããå¿ èŠããããŸãã
java.util.Calendar
@Test public void testCalendarMs() throws Exception { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); Calendar calendar = Calendar.getInstance(tz); calendar.setLenient(false); calendar.set(2016, Calendar.APRIL, 20, 12, 0, 0); System.out.println(calendar.getTimeInMillis()); calendar.set(Calendar.MILLISECOND, 0); System.out.println(calendar.getTimeInMillis()); }
1461142800808 1461142800000
æåã®çªå·ã¯ãåŒã³åºãã®æéã«å¿ããŠãæåã®3æ¡ã«ãªããŸãã ãã®åäœã¯ããã¹ãã§ã¯éåžžã«éèŠã§ãã
ã¿ã€ã ã¹ã¿ã³ãã軞äžã®ãã€ã³ãã«ããŸãã¯ãã®éã«å€æããããã«ãã¯ã©ã¹java.text.DateFormatãšãã®åå«ããããŸãã
java.text.DateFormat
@Test public void testFormat() throws Exception { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setLenient(false); dateFormat.setTimeZone(tz); Date moment = dateFormat.parse("2005-03-27 01:30:00"); Assert.assertEquals("2005-03-27 01:30:00", dateFormat.format(moment)); }
java.text.DateFormatããã³java.util.Calendarã«ã€ããŠã¯ã次ã®ããšãæ確ã«èšãå¿ èŠããããŸãã
- äž¡æ¹ã®ã¯ã©ã¹ã«ã¯ãã¿ã€ã ãŸãŒã³ãæ瀺çã«èšå®ããããã®setTimezoneïŒïŒã¡ãœããããããŸãã ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ã«äŸåããã®ã§ã¯ãªããããã»ã¹ãå®å šã«å¶åŸ¡ããŠããããšã瀺ãããã«åžžã«äœ¿çšããããšã匷ããå§ãããŸãã
- äž¡æ¹ã®ã¯ã©ã¹ã«ã¯ããœããã¢ãŒããèšå®ããããã®setLenientïŒïŒã¡ãœããããããŸãã ãã®ã¢ãŒãã§ã¯ãäž¡æ¹ã®ã¯ã©ã¹ãã«ã¬ã³ããŒã©ãã«ã®ãšã©ãŒã蚱容ããŠãå®éã®æå³ãæšæž¬ããããšããŸãã ç¶æ³ã«ãã£ãŠç°ãªããŸãããæšæž¬ãç¡å¹ã«ããããšããå§ãããŸãïŒããã©ã«ãã§ã¯ãããœãããã¢ãŒãããªã³ã«ãªã£ãŠããŸãïŒã
- äž¡æ¹ã®ã¯ã©ã¹ã¯ã¹ã¬ããã»ãŒãã§ãã ãŸããjava.util.Calendarã®å Žåããããå®å šã«äºæ³ãããå ŽåïŒå éšç¶æ ãããããšãããã£ãŠããããïŒãjava.text.DateFormatã®å Žåãããã¯å€ãã®äººã«ãšã£ãŠé©ãã§ãã
ïŒå€ãIPAã«ã¯ïŒããã«ããã€ãã®ã¯ã©ã¹ããããŸãïŒ
java.sql.Timestamp-ããŒã¿ããŒã¹ã®TIMESTAMPåãæäœããããã®ããç§ç²ŸåºŠã®java.util.Dateã®æ¡åŒµïŒãµãã¯ã©ã¹ïŒ
java.sql.Date-ããŒã¿ããŒã¹ã®DATEåãæäœããããã®java.util.Dateã®æ¡åŒµïŒãµãã¯ã©ã¹ïŒã
java.sql.Time-ããŒã¿ããŒã¹ã®TIMEåãæäœããããã®java.util.Dateã®æ¡åŒµïŒãµãã¯ã©ã¹ïŒã
ããã«ãä»»æã®æç¹ããããªç§åäœã®éåžžã®Unixæéã®é·ãå€ãšããŠä¿åã§ããŸãã
ã¿ã€ã ãŸãŒã³
äžçäžïŒãŸãã¯ãã·ã¢å šäœïŒã«å€ãã®ãªãã£ã¹ãæã€ã°ããŒãã«äŒæ¥ã§åããçµéšã®ãã人ãªã誰ã§ãããäŒè°ã¯2016幎1æ1æ¥14:00:00ã«éå¬ãããŸãããšãããã¬ãŒãºã«å«ãŸããæ å ±ã¯äºå®äžåœ¹ã«ç«ããªãããšãç¥ã£ãŠããŸãã ã14:00ããšããããŒã¯ã¯ãæé軞äžã®ç¹å®ã®ãã€ã³ãã«å¯Ÿå¿ãããã®ã§ã¯ãªããè€æ°ã®ãã€ã³ãã«å¯Ÿå¿ãããã®ã§ãã å šå¡ãåæã«ãããªãã£ããã«ãŒã ã«éãŸãããã«ããããã«ãäŒè°ã®äž»å¬è ã¯å¥ã®äœããã€ãŸãã14:00ãããŒã¯ã解éããã¿ã€ã ãŸãŒã³ãæå®ããå¿ èŠããããŸãã å€ãã®å Žåãã¿ã€ã ãŸãŒã³ã¯æ¬éšæ¬éšã«ãã£ãŠæ瀺ãããŸãïŒãã¢ã¹ã¯ã¯æéã«åŸã£ãŠäœæ¥ãããïŒãããã§ãªãå Žåãããã©ã«ãã®ã¿ã€ã ãŸãŒã³ããŸã£ããæ瀺ãããªãå Žåã¯ãã2016幎1æ1æ¥14:00ã«ãæ瀺çã«èšå®ããå¿ èŠããããŸãïŒ00 MSK ''-ãã®å Žåãæé軞äžã®ãã€ã³ãã¯å®å šã«æ確ã«èšå®ãããå šââå¡ãåæã«äŒè°ã«éãŸããŸãã
HHïŒMMïŒCC圢åŒã®ã¿ã€ã ã¹ã¿ã³ãã䜿çšããæäœã®ãããŸãããæé€ããã«ã¯ãã¿ã€ã ã¹ã¿ã³ãã®è¡šç€ºæãšå ¥åæã®äž¡æ¹ã§ã¿ã€ã ãŸãŒã³ãæå®ããå¿ èŠããããŸãã
äœããã®æ¹æ³ã§æé»çã«æ瀺ã§ããå Žåãã¿ã€ã ãŸãŒã³ãæ瀺çã«æå®ããããšã¯ã§ããŸããã
- ã¿ã€ã ãŸãŒã³ã¯ãããã©ã«ãã§ã¯ãµãŒãã¹å šäœã§åãã§ãããšæ³å®ãããŠããŸãã
- ã¿ã€ã ãŸãŒã³ã¯ããŠãŒã¶ãŒããããã¡ã€ã«ã§æ瀺çã«æå®ããŸãã
- ãŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ã¯ãå°ç座æšãä»ããäœçœ®ã«ãã£ãŠèšç®ã§ããŸãã
- ãŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ã¯ãIPã¢ãã¬ã¹ãä»ãããŠãŒã¶ãŒã®äœçœ®ã«ãã£ãŠèšç®ã§ããŸãã
- ãŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ã¯ãéæ¥çãªæ©èœïŒåäœåæïŒãä»ããŠãŠãŒã¶ãŒã®äœçœ®ã«ãã£ãŠèšç®ã§ããŸãã
- ãŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ã®çŸåšã®ãªãã»ããã¯ã JavaScriptã§èšç®ã§ããŸã ã
éåžžããµãŒãã¹ã®ã¿ã€ã ãŸãŒã³ïŒããã©ã«ãïŒãšãµãŒããŒã®ã¿ã€ã ãŸãŒã³ïŒããã©ã«ãïŒã¯åãã§ã¯ãªãããšã«æ³šæããŠãã ããã BIOSãã·ã¹ãã ãã¢ããªã±ãŒã·ã§ã³ãããã³ãŠãŒãã£ãªãã£ã¯ãããšãã°ãUTCã¿ã€ã ãŸãŒã³ã§åäœã§ããŸãããåæã«ããµãŒãã¹ã®ã¿ã€ã ãŸãŒã³ã¯ã¢ã¹ã¯ã¯ã®ã¿ã€ã ãŸãŒã³ã«ãªããŸãã ãã¡ãããäžèŽããå Žåã¯ã¯ããã«ç°¡åã§ããããã®å Žåã管çè ã¯ãµãŒããŒã«ã¿ã€ã ãŸãŒã³ãèšå®ããããã°ã©ããŒã¯ãããã«ã€ããŠãŸã£ããèããŸããã ãããã¡ããã©ããªãã®å Žåã§ãããªããããªãã¯ãã以äžèªãããšãã§ããŸããã
åãã¿ã€ã ã¹ã¿ã³ããããŸããŸãªæ¹æ³ã§ãŠãŒã¶ãŒã«è¡šç€ºã§ããŸãã誰ããæã䜿ãæ £ããã¿ã€ã ãŸãŒã³ã䜿çšããŸãã ããšãã°ã次ã®ã¿ã€ã ã¹ã¿ã³ãã¯ãæé軞äžã®åãæç¹ãæããŸãã
Fri Jan 1 16:29:00 MSK 2016 Fri Jan 1 13:29:00 UTC 2016 Fri Jan 1 14:29:00 CET 2016 Fri Jan 1 21:29:00 SGT 2016 Fri Jan 1 22:29:00 JST 2016
ã¿ã€ã ãŸãŒã³ã«é¢ãããŠã£ãããã£ã¢ ã
Javaã§ã¯ãã¿ã€ã ãŸãŒã³æ å ±ã¯java.util.TimeZoneã¯ã©ã¹ã§è¡šãããŸãã
ã¿ã€ã ãŸãŒã³ã¯ãªãã»ããã§ã¯ãªããšèšããªããã°ãªããŸããã ããã¯ãGMT + 3ããšãŒããã/ã¢ã¹ã¯ã¯ã§ãããšèšã£ãŠããããã§ã¯ãããŸããã ãããã2016å¹Žå šäœã§ãã¿ã€ã ãŸãŒã³ãšãŒããã/ã¢ã¹ã¯ã¯ã¯GMT + 3ãªãã»ããã«å¯Ÿå¿ãããšèšããŸãã ã¿ã€ã ãŸãŒã³ã¯ãå±¥æŽæéå šäœã®å€äœã®å®å šãªå±¥æŽãããã³ããŸããŸãªéå»ã®ç¬éã®å€äœãæ£ç¢ºã«èšç®ããæ£ããæéèšç®ãè¡ãããšãã§ããä»ã®ããŒã¿ã§ãã
ã¿ã€ã ãŸãŒã³ã調ã¹ãŠã¿ãŸããã-æåã«ãšãŒããã/ã¢ã¹ã¯ã¯ãèŠãŠã¿ãŸãããïŒ
ãšãŒããã/ã¢ã¹ã¯ã¯
@Test public void testTzMoscow() throws Exception { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); System.out.println(tz.getRawOffset()); System.out.println(tz.getOffset(System.currentTimeMillis())); System.out.println(tz.useDaylightTime()); System.out.println(tz.getDisplayName(false, TimeZone.LONG, Locale.ENGLISH)); System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.ENGLISH)); System.out.println(tz.getDisplayName(true, TimeZone.LONG, Locale.ENGLISH)); System.out.println(tz.getDisplayName(true, TimeZone.SHORT, Locale.ENGLISH)); System.out.println(tz.getDisplayName(false, TimeZone.LONG, Locale.FRENCH)); System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.FRENCH)); System.out.println(tz.getDisplayName(true, TimeZone.LONG, Locale.FRENCH)); System.out.println(tz.getDisplayName(true, TimeZone.SHORT, Locale.FRENCH)); }
10,800,000 10,800,000 åœ ã¢ã¹ã¯ã¯æšæºæ Msk ã¢ã¹ã¯ã¯å€æé Msd Heure Standard de moscou Msk Heureavancéede moscou Msd
ãšãŒããã/ã¢ã¹ã¯ã¯ãŸãŒã³ã§ã¯ãçŸåšã®ããŒã¹ãªãã»ãããUTCã«å¯ŸããŠ+3æéã§ãããçŸæç¹ã§ã®åèšãªãã»ããã+3æéã§ããããšãããããŸãã å°æ¥ãå€æéã®ç¢å°ã®ç¿»èš³ã¯ãããŸããã ãŸãŒã³ã«ã¯ãå€æéãšå¬æéã®å¥ã ã®ååããããŸãã
ããªã®æéãèŠãŠã¿ãŸãããã
ãšãŒããã/ããª
@Test public void testTzParis() throws Exception { TimeZone tz = TimeZone.getTimeZone("Europe/Paris"); System.out.println(tz.getRawOffset()); System.out.println(tz.getOffset(System.currentTimeMillis())); System.out.println(tz.useDaylightTime()); System.out.println(tz.getDisplayName(false, TimeZone.LONG, Locale.ENGLISH)); System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.ENGLISH)); System.out.println(tz.getDisplayName(true, TimeZone.LONG, Locale.ENGLISH)); System.out.println(tz.getDisplayName(true, TimeZone.SHORT, Locale.ENGLISH)); System.out.println(tz.getDisplayName(false, TimeZone.LONG, Locale.FRENCH)); System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.FRENCH)); System.out.println(tz.getDisplayName(true, TimeZone.LONG, Locale.FRENCH)); System.out.println(tz.getDisplayName(true, TimeZone.SHORT, Locale.FRENCH)); }
3,600,000 3,600,000 æ¬åœ äžå€®ãšãŒãããæé ã»ã äžå€®ãšãŒãããå€æé ã»ã¹ã äžå€®ãšãŒããã ã»ã ãšãŒãããã®äžå€®é§ ã»ã¹ã
ããŒã¹ãªãã»ããã¯UTCã«å¯ŸããŠ+1æéã§ãçŸåšã®åèšãªãã»ããã+1æéã§ãã ãŸãŒã³ã®å°æ¥ã®å€æéãžã®ç§»è¡ã å¬ãšå€æéã«å¥ã ã«ã2ã€ã®ååãå²ãåœãŠãããŸãã
次ã«ããŸãŒã³ãGMT + 5ããèŠãŠã¿ãŸãããã ããã¯å®éã«ã¯äžæçãªãŸãŒã³ã§ã¯ãããŸãã-æŽå²ãå€æéããªãããã·ããã¯äžå®ã§ãã
GMT + 5
@Test public void testGmt5() throws Exception { TimeZone tz = TimeZone.getTimeZone("GMT+5"); System.out.println(tz.getRawOffset()); System.out.println(tz.getOffset(System.currentTimeMillis())); System.out.println(tz.useDaylightTime()); System.out.println(tz.getDisplayName(false, TimeZone.LONG, Locale.ENGLISH)); System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.ENGLISH)); System.out.println(tz.getDisplayName(true, TimeZone.LONG, Locale.ENGLISH)); System.out.println(tz.getDisplayName(true, TimeZone.SHORT, Locale.ENGLISH)); System.out.println(tz.getDisplayName(false, TimeZone.LONG, Locale.FRENCH)); System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.FRENCH)); System.out.println(tz.getDisplayName(true, TimeZone.LONG, Locale.FRENCH)); System.out.println(tz.getDisplayName(true, TimeZone.SHORT, Locale.FRENCH)); }
18,000,000 18,000,000 åœ GMT + 05ïŒ00 GMT + 05ïŒ00 GMT + 05ïŒ00 GMT + 05ïŒ00 GMT + 05ïŒ00 GMT + 05ïŒ00 GMT + 05ïŒ00 GMT + 05ïŒ00
ã€ãŸãããã€ã¢ã¹ã¯äžå®ã§ãGMTãšæ¯èŒããŠ+5æéã§ãããå€åããããšã¯ãããŸããã
äŸ
ç§ãã¡ã¯èšèã§èª¬æããã«ã¯é·ããããã®ãäŸã§èªãç¶ããŸãã ãŸãã2005幎ã«ããšãŒããã/ã¢ã¹ã¯ã¯ãã®ã¿ã€ã ãŸãŒã³ã§äœãèµ·ãã£ãã®ããèŠãŠã¿ãŸãããã
ãšãŒããã/ã¢ã¹ã¯ã¯-2005
$ zdump -v /usr/share/zoneinfo/Europe/Moscow | grep 2005 /usr/share/zoneinfo/Europe/Moscow Sat Mar 26 22:59:59 2005 UT = Sun Mar 27 01:59:59 2005 MSK isdst=0 gmtoff=10800 /usr/share/zoneinfo/Europe/Moscow Sat Mar 26 23:00:00 2005 UT = Sun Mar 27 03:00:00 2005 MSD isdst=1 gmtoff=14400 /usr/share/zoneinfo/Europe/Moscow Sat Oct 29 22:59:59 2005 UT = Sun Oct 30 02:59:59 2005 MSD isdst=1 gmtoff=14400 /usr/share/zoneinfo/Europe/Moscow Sat Oct 29 23:00:00 2005 UT = Sun Oct 30 02:00:00 2005 MSK isdst=0 gmtoff=10800
ããŠãæãå€ã«åããå¬ã«æ»ãã®ãããããŸãã ã¿ã€ã ã¹ã¿ã³ãã§ãããã®æéã«äœãèµ·ãããèŠãŠã¿ãŸãããã æå§ãã«-å¬æéãžã®ç§»è¡ïŒ
å¬æé
@Test public void testWinterTime() throws Exception { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); dateFormat.setLenient(false); dateFormat.setTimeZone(tz); Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2005, Calendar.OCTOBER, 29, 22, 0, 0); for (int i = 0; i < 62; i++) { String mark = dateFormat.format(calendar.getTime()); System.out.printf("%s - %d, %s\n", mark, tz.getOffset(calendar.getTimeInMillis()), tz.inDaylightTime(calendar.getTime())); calendar.add(Calendar.MINUTE, +1); } }
2005-10-30 02:00:00 MSD-14400000ãtrue 2005-10-30 02:01:00 MSD-14400000ãtrue ... 2005-10-30 02:58:00 MSD-14400000ãtrue 2005-10-30 02:59:00 MSD-14400000ãtrue 2005-10-30 02:00:00 MSK-10,800,000ãfalse 2005-10-30 02:01:00 MSK-10,800,000ãfalse
02:59:00 MSDåŸãç¢å°ã¯1æéæ»ãã次ã®ããŒã¯ã¯ãã§ã«02:00:00 MSK-å¬æéã§ãã ã¿ã€ã ãŸãŒã³ã¯ãå€æéãçµäºãããªãã»ãããGMT + 4ããGMT + 3ã«å€æŽãããããšã瀺ããŸãã
ãã®äŸã«ã¯èå³æ·±ããã¥ã¢ã³ã¹ããããŸãããšãŒããã/ã¢ã¹ã¯ã¯ãŸãŒã³ã䜿çšããå Žåã02ïŒ00ïŒ00 MSDããŒã¯ã«å¯Ÿå¿ããã«ã¬ã³ããŒäžã®ãã€ã³ããèšå®ããããšã¯çµ¶å¯Ÿã«äžå¯èœã§ãããã€ã³ãã¯å¿ èŠãª1æéåŸã®02:00:00 MSKã«èšå®ãããŸãã ãã®ãã€ã³ããåç §ãã€ã³ããšããŠèšå®ããã«ã¯ããã¹ãŠãèšå®ã§ããUTCã¿ã€ã ãŸãŒã³ã®ãµãŒãã¹ã«é Œãå¿ èŠããããŸãã å¥ã®ãªãã·ã§ã³ã¯ããšãŒããã/ã¢ã¹ã¯ã¯ãŸãŒã³ã§MSDãã€ã³ã01:00:00ãèšå®ããæéãè¿œå ããããšã§ãã
çŸåš-å€æéïŒ
å€æé
@Test public void testSummerTime() { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); dateFormat.setLenient(false); dateFormat.setTimeZone(tz); Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2005, Calendar.MARCH, 26, 22, 0, 0); for (int i = 0; i <= 60; i++) { String mark = dateFormat.format(calendar.getTime()); System.out.printf("%s - %d, %s\n", mark, tz.getOffset(calendar.getTimeInMillis()), tz.inDaylightTime(calendar.getTime())); calendar.add(Calendar.MINUTE, +1); } }
2005-03-27 01:00:00 MSK-10,800,000ãfalse 2005-03-27 01:01:00 MSK-10,800,000ãfalse ... 2005-03-27 01:58:00 MSK-10,800,000ãfalse 2005-03-27 01:59:00 MSK-10,800,000ãåœ 2005-03-27 03:00:00 MSD-14400000ãtrue 2005-03-27 03:00:01 MSD-14400000ãtrue
01:59:00以éãMSKã¯03:00:00 MSDã®çŽåŸã«ç¶ããŸããã€ãŸããéã1æéå ã«ç§»åããŸãã ã¿ã€ã ãŸãŒã³ã¯ããã®æç¹ã§ãªãã»ãããGMT + 3ããGMT + 4ã«å€åããããšãéç¥ããå€æéãã©ã°ã衚瀺ãããŸãã
ããããããšãŒããã/ã¢ã¹ã¯ã¯ããŸãŒã³ã§ã2005-03-27 02:30:00ããšããã©ãã«ãåŠçããããšãããšã©ããªããŸããïŒçè«çã«ã¯ããã®ãããªã©ãã«ã¯ååšãã¹ãã§ã¯ãããŸãããïŒ
ååšããªãã©ãã«
@Test public void testMissing() throws Exception { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setLenient(false); dateFormat.setTimeZone(tz); Date moment = dateFormat.parse("2005-03-27 02:30:00"); System.out.println(moment); }
java.text.ParseExceptionïŒè§£æäžèœãªæ¥ä»ïŒ "2005-03-27 02:30:00"
ããã§ã-å³æ Œã¢ãŒãã§ã¯äŸå€ãçºçããŸãã
æãå¬æéã«åãæ¿ãã£ãæ¥ã®1æ¥ã®ç¶ç¶æéãèšç®ããŸãã
å¬æéã®å€æ
@Test public void testWinterDay() { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTimeZone(tz); calendar.set(2005, Calendar.OCTOBER, 30, 0, 0, 0); Date time1 = calendar.getTime(); calendar.add(Calendar.DAY_OF_YEAR, +1); Date time2 = calendar.getTime(); System.out.println(TimeUnit.MILLISECONDS.toHours(time2.getTime() - time1.getTime())); }
25
2005-10-30 00:00:00 MSDãã2005-10-31 00:00:00 MSKãŸã§ã24æéã§ã¯ãªã25æéãçµéããŸããã
次ã«ãå€æéã確èªããŸãã
å€æé
@Test public void testSummerDay() { TimeZone tz = TimeZone.getTimeZone("Europe/Moscow"); Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTimeZone(tz); calendar.set(2005, Calendar.MARCH, 27, 0, 0, 0); Date time1 = calendar.getTime(); calendar.add(Calendar.DAY_OF_YEAR, +1); Date time2 = calendar.getTime(); System.out.println(TimeUnit.MILLISECONDS.toHours(time2.getTime() - time1.getTime())); }
23
2005-03-27 00:00:00 MSKãã2005-03-28 00:00:00 MSDãŸã§ã24æéã§ã¯ãªã23æéçµéããŸããã
ãããã®æåŸã®2ã€ã®äŸã¯ã24 * 60 * 60 * 1000ããªç§ã24æéã§ã¯ãªãã«ã¬ã³ããŒæ¥ãšããŠè¿œå ãã人å°çšã§ãã å€æéãšå¬æéã®ä¹ãæãã¯ãããªãã®ã§ãä»ã¯ãã®ãããªåé¡ã¯ãªããšèšããã§ãããã ããã«ç§ã¯ä»¥äžã«çããããšãã§ããŸãïŒ
- ããã°ã©ã ã¯ããšãŒããã/ã¢ã¹ã¯ã¯ã®ãŸãŒã³ã ãã§ãªããã©ã®ã¿ã€ã ãŸãŒã³ã§ãæ£ããåäœããã¯ãã§ãã
- ãéå»ããžã®ãåŸæ¹ãèšç®ã«ã¯ãäŸç¶ãšããŠæ£ããã¢ãããŒããå¿ èŠã§ãã
- 2016幎ã«ã¯å·äžé¢éžæãè¡ããã2018幎ã«ã¯å€§çµ±é éžæãè¡ãããŸãããã®ããã話ã¯ãŸã çµãã£ãŠããªããšæããŸãã
java.sql.Timeãjava.sql.Date
åã¯ãããããSQLåã®TIMEããã³DATEã§åäœããããã«èšèšãããŠããŸãã äž¡æ¹ã®å€ã¯æé垯ã«äŸåããªãããšãç解ãããŠããŸãããæ®å¿µãªããããã¯å®å šã«çå®ã§ã¯ãããŸããã äž¡æ¹ã®ã¿ã€ãã¯java.util.Dateã®åå«ã§ãããããæ¥æ°ã®è§£éã¯ã¿ã€ã ãŸãŒã³ã«äŸåããŸãã
java.sqlåã®åé¡
@Test public void testSqlTime() throws Exception { // 2015-01-01 01:00:00 MSK Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("Europe/Moscow")); calendar.setTimeInMillis(0); calendar.set(2015, Calendar.JANUARY, 10, 1, 0, 0); long now = calendar.getTimeInMillis(); // java.sql.Time java.sql.Time sqlTime = new java.sql.Time(now); java.sql.Date sqlDate = new java.sql.Date(now); // Europe/London DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); timeFormat.setTimeZone(TimeZone.getTimeZone("Europe/London")); Assert.assertEquals("22:00:00", timeFormat.format(sqlTime)); // Europe/London DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London")); Assert.assertEquals("2015-01-09", dateFormat.format(sqlDate)); }
éåžžãã³ãŒãã¯åãã¿ã€ã ãŸãŒã³ã®äž¡æ¹ã§æ©èœãããããã©ã¡ãã®ã¿ã€ããããžãã¹ããžãã¯ããJDBCãã©ã€ããŒã«æ å ±ã転éããã¿ã¹ã¯ã«å¯ŸåŠããŸãããã·ãªã¢ã«åãªã©ã®ããé«åºŠãªã±ãŒã¹ã§ã¯ããããã®ã¯ã©ã¹ã䜿çšããå Žåã¯ååã«æ³šæããå¿ èŠããããŸã
察å¿ããã¿ã€ãã®æ°ããAPIã§ã¯ããã®ãããªåé¡ã¯è§£æ±ºãããŠããŸãã
UTCãGMT
ã»ãšãã©ã®äººã¯ãGMTãšUTCã¯ä»ã®ã¿ã€ã ãŸãŒã³ã§åœ¢æããããªãã»ããã«é¢é£ããç¹å¥ãªæå®ã§ããããšãç¥ã£ãŠããŸãã ãããã誰ããUTCãšGMTããŸã£ããåããã®ã§ã¯ãªãããšãç¥ã£ãŠããããã§ã¯ãããŸãã ïŒæ£åŒã«ïŒã ã€ãŸããã©ãã«ã2015-12-01 00:00:00 GMTããšã2015-12-01 00:00:00 UTCãã¯ãæé軞äžã®ç°ãªãïŒè¿ãïŒãã€ã³ãã瀺ããŠããŸãã
GMT㯠ãä»ã®ãªããžã§ã¯ãã«å¯Ÿããå°çã®äœçœ®ãã倩æåŠçã«èšç®ãããŸãã GMTã¯ãäžéšã®åœã§ã¯ã¿ã€ã ãŸãŒã³ãšããŠçŽæ¥äœ¿çšãããŸãã
å°çã®å転ã¯ã©ã³ãã ã«æžéãããããå°çã¯åžžã«å¢å ããæéééã§åãäœçœ®ã«ãããŸãã ãããã£ãŠãé£æ¥ããGMTããŒã¯äžã®æå»ãã€ã³ãéã®è·é¢ïŒããšãã°ãã10:00:01ããšã10:00:02ãïŒã¯1 ç§ãšæ£ç¢ºã«äžèŽããªãå ŽåããããŸãã
UTC㯠GMTã®ä»£ããã«å°å ¥ãããååæéã§èšç®ãããŸãã ã¿ã€ã ãŸãŒã³ãšããŠçŽæ¥äœ¿çšãããããšã¯ãããŸããïŒãã£ã¹ãã¬ã€ã¹ã¡ã³ãã®ãµããŒããšããŠã®ã¿ïŒã
UTCã§ã¯ãã¿ã€ã ã¹ã¿ã³ãéã®è·é¢ïŒããšãã°ãã10:00:01ããšã10:00:02ãïŒã¯ãŸã£ããåãã§ãå³å¯ã«ã¯1ç§ã«çãããªããŸãã å°çã®èªè»¢ã®é ããšGMTãšã®çŽ¯ç©çãªå·®ã¯ã1幎ïŒãŸãã¯2ç§ïŒã®äœåãªç§ãã€ãŸããããç§ãå ¥åããããšã§è§£æ±ºãããŸãã
ãããã£ãŠãGMTãšUTCã§åãããŒã¯ã®ãããã€ã³ãã®å·®ã1ç§ãè¶ ããããšã¯ãããŸããã
圌ã㯠ãUTCãGMTãã»ãŒå®å šã«çœ®ãæããGMT + 3ã®åœ¢åŒã§ãªãã»ããè¡šèšã䜿çšããããšã¯é·ããæªãèãã ãšæžããŠããŸããUTC+ 3è¡šèšã䜿çšããã®ã¯æ£ããããšã§ãã
GMTãUTCãå€æéã¯ãããŸããã
Javaã§äœ¿çšãããUnix-time㯠ãUTCãŸãã¯GMTã«çŽæ¥å¯Ÿå¿ããŠããªããšèšããªããã°ãªããŸããã äžæ¹ã§ã¯ãUnixæéã§ã¯ãé£æ¥ããã©ãã«éã®å·®ã¯åžžã«1ç§ã§ãããä»æ¹ã§ã¯ãUnixæéã§ã¯leç§ã¯æ³å®ãããŠããŸããã
ããã©ã«ãã®ã¿ã€ã ãŸãŒã³
åºåã«ã¿ã€ã ãŸãŒã³ãæ瀺çã«è¡šç€ºãããã衚瀺ããªãããå ¥åæã«ã¿ã€ã ãŸãŒã³ãèŠæ±ããããæéã©ããã®æäœäžã«ã¿ã€ã ãŸãŒã³ãæå®ãããæå®ããªãããèŠæ±ããŸãããïŒãããã®æäœã«ã¯æé»çã«äžéšã®ã¿ã€ã ãŸãŒã³ãååšããŠããŸãã ç¬èªã®ãã®ãæå®ããªãã£ãå Žåãããã©ã«ãã®ã¿ã€ã ãŸãŒã³ã䜿çšãããŸãã
ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ãšããçšèªã¯ãäžèšã®ããã¹ãã§ãã§ã«äœåºŠãèšåãããŠããŸãã ãªããªãããã®æŠå¿µããªããã°ãå®éã«ã¯äœã説æã§ããªãããã§ãã ã¿ã€ã ã¹ã¿ã³ãã®æéãåºåãããã³å ¥åã䌎ããã¹ãŠã®æäœã«ã¯ã¿ã€ã ãŸãŒã³ãå¿ èŠã§ãã ããªãããããæå®ããªããšããäºå®ã¯ããããããã«ãªãããšãæå³ããªã-ããã¯åã«ããã©ã«ãã§åãããã
ãããããã¹ãŠãåã³ããã»ã©åçŽã§ã¯ãããŸãã-ããã©ã«ãã§ã¯èª°ã®ããã«ããªãã§ããïŒ
ã«ãŒãã«ããå§ããŸãããã hwclockããã¥ã¢ã«ã§ã¯ãã«ãŒãã«ã«ã¯ã¿ã€ã ãŸãŒã³ã®å éšæŠå¿µããããŸãããFATãã¡ã€ã«ã·ã¹ãã ãã©ã€ããŒã®ãããªããã€ãã®çããã¢ãžã¥ãŒã«ãé€ããŠãã»ãšãã©èª°ã䜿çšããŠããŸããã åãhwclockã³ãã³ãã䜿çšããŠãã¿ã€ã ãŸãŒã³ã®å€æŽã«ã€ããŠã«ãŒãã«ã«éç¥ã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ã¢ããªã±ãŒã·ã§ã³ã¯ãããã€ãã®æ¹æ³ã§ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ãå®çŸ©ããŸãã
ãŸããUbuntuã®ã·ã¹ãã å šäœã®ã¿ã€ã ãŸãŒã³ïŒããã«é¢ããå®å šãªæ å ±ïŒã¯ããã¡ã€ã«ïŒã·ã³ããªãã¯ãªã³ã¯ïŒ/ etc / localtimeã«ä¿åããããã®ã¿ã€ã ãŸãŒã³ã®ååã¯ãã¡ã€ã«/ etc / timezoneã«ãããŸãã
$ cat /etc/timezone Europe/Moscow $ file /etc/localtime /etc/localtime: timezone data, version 2, 15 gmt time flags, 15 std time flags, no leap seconds, 77 transition times, 15 abbreviation chars $ zdump -v /etc/localtime | head -n 10 /etc/localtime -9223372036854775808 = NULL /etc/localtime -9223372036854689408 = NULL /etc/localtime Wed Dec 31 21:29:42 1879 UT = Wed Dec 31 23:59:59 1879 LMT isdst=0 gmtoff=9017 /etc/localtime Wed Dec 31 21:29:43 1879 UT = Thu Jan 1 00:00:00 1880 MMT isdst=0 gmtoff=9017 /etc/localtime Sun Jul 2 21:29:42 1916 UT = Sun Jul 2 23:59:59 1916 MMT isdst=0 gmtoff=9017 /etc/localtime Sun Jul 2 21:29:43 1916 UT = Mon Jul 3 00:01:02 1916 MMT isdst=0 gmtoff=9079 /etc/localtime Sun Jul 1 20:28:40 1917 UT = Sun Jul 1 22:59:59 1917 MMT isdst=0 gmtoff=9079 /etc/localtime Sun Jul 1 20:28:41 1917 UT = Mon Jul 2 00:00:00 1917 MST isdst=1 gmtoff=12679 /etc/localtime Thu Dec 27 20:28:40 1917 UT = Thu Dec 27 23:59:59 1917 MST isdst=1 gmtoff=12679 /etc/localtime Thu Dec 27 20:28:41 1917 UT = Thu Dec 27 23:00:00 1917 MMT isdst=0 gmtoff=9079
Ubuntuã®å Žåããã£ã¹ããªãã¥ãŒã·ã§ã³çšã®ç¹å¥ãªã³ãã³ãã䜿çšããŠã·ã¹ãã ã®ã¿ã€ã ãŸãŒã³ãèšå®ã§ããŸãã
$ dpkg-reconfigure tzdata
ãã©ã€ãtzselectãŠãŒãã£ãªãã£ããããŸãã
$ tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent, ocean, "coord", or "TZ". 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean 10) Pacific Ocean 11) coord - I want to use geographical coordinates. 12) TZ - I want to specify the time zone using the Posix TZ format. #?
ã¿ã€ã ãŸãŒã³ãæå®ãã2çªç®ã®æ¹æ³ã¯ãç°å¢å€æ°TZã§ãããã®ç°å¢å€æ°ã§ã¯ãåããã°ã©ã ããŠãŒã¶ãŒããšã«ã¿ã€ã ãŸãŒã³ã®èå¥åãåå¥ã«æå®ã§ããŸãã
$ echo $TZ $ date Wed Dec 30 20:18:18 MSK 2015 $ TZ=UTC date Wed Dec 30 17:18:25 UTC 2015 $ TZ=Europe/London date Wed Dec 30 17:18:35 GMT 2015 $ TZ=Europe/Paris date Wed Dec 30 18:18:40 CET 2015
äžéšã®ããã°ã©ã ã§ã¯ãèšå®ããã³/ãŸãã¯ã³ãã³ãã©ã€ã³åŒæ°ã§ç¹å®ã®ã¿ã€ã ãŸãŒã³ãèŠæ±ã§ããŸãã
$ date --utc Fri Jan 1 08:34:36 UTC 2016
ãšããã§ãæéãªãã§çŸåšã®ã¿ã€ã ãŸãŒã³ã®ã¿ã衚瀺ããããã«æ¥ä»ãèŠæ±ã§ããŸãïŒ
$ date +%Z MSK
ããããããã¯libcã§ã®éåžžã®ããã°ã©ã çšã§ãããJavaãã©ãããã©ãŒã å šäœããããŸãããããã£ãŠãäžèšã®2ã€ã®å¯èœæ§ã«å ããŠãããã«2ã€ãããŸãã
JVMãèµ·åããåŒæ°ãæå®ã§ããŸãã
$ cat << EOF | scala -Duser.timezone=Europe/Paris print("%s\n%s\n".format(java.util.TimeZone.getDefault().getID(), new java.util.Date())) EOF ... Europe/Paris Wed Dec 30 19:24:00 CET 2015
ãŸããTimeZone.setDefaultïŒTimeZone timeZoneïŒã¡ãœããã䜿çšããŠãã³ãŒãã§ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ãçŽæ¥èšå®ã§ããŸãã
$ cat << EOF | scala > java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/Los_Angeles")) > print("%s\n%s\n".format(java.util.TimeZone.getDefault().getID(), new java.util.Date())) > EOF ... America/Los_Angeles Wed Dec 30 10:25:45 PST 2015
ãŸãã¯äžåºŠã«ãã¹ãŠïŒ
$ TZ=Europe/London cat << EOF | scala -Duser.timezone=Europe/Paris java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/Los_Angeles")) print("%s\n%s\n".format(java.util.TimeZone.getDefault().getID(), new java.util.Date())) EOF ... America/Los_Angeles Wed Dec 30 10:37:28 PST 2015
ã¿ã€ã ãŸãŒã³ããŒã¹
ããŸããŸãªåœãå°åã®è°å¡ãæ¿åºã¯ãå€æéãå®æçã«ãªã³/ãªãã«ããããã¿ã€ã ãŸãŒã³éã§å°åã移åãããããããšãããªããã¢ã€ãã«ç¶æ ã§ã¯ãããŸããããã®ãããªå€æŽã«é¢ãããã¹ãŠã®ææ°æ å ±ãã·ã¹ãã ã«ä¿æããããšãéèŠã§ããããããªããšãæå»ãæ£ããå ¥åããã³è¡šç€ºããããç¡ç äžã«SMSãåä¿¡ãããã+ 2æŠæ¥ããªã©ã®èšç®ãæ£ãããªããªããŸãã
Linuxã§ã¯ãéåžžã®libcããã°ã©ã ã¯/ usr / share / zoneinfoãã£ã¬ã¯ããªå ã®ãã¡ã€ã«ã§æ§æãããã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹ã䜿çšããŸãããããã®ãã¡ã€ã«ã¯tzdataããã±ãŒãžã«å±ããåãã£ã¹ããªãã¥ãŒã·ã§ã³ã®éçºè ãç©æ¥µçã«ç£èŠããŠããŸãããã®ããã±ãŒãžã¯ã¿ã€ã ãªãŒã«æŽæ°ãããŸãããåé¡ã¯èŠããŠããŸãããæåŸã®æ段ãšããŠããããã€ãããããŒãžã§ã³ã®Linuxã誰ã§ããµããŒããããªããªã£ãå Žåããã¹ãŠã®æ å ±ãæåã§æŽæ°ã§ããŸãã
幞ããªããšã«ããããã®ãã¡ã€ã«ã®ã³ã³ãã³ãã®åœ¢åŒãããããã®çºçã®å±¥æŽãããã§æãå¿ èŠã¯ãããŸãã-Habréã«ã¯æ¢ã«ãã®ãããã¯ã«é¢ããåªããèšäºãããããã§ããWikipediaã«ãåªããèšäºããããŸãã
ããããããã»ã©åçŽã§ã¯ãããŸããã
Javaã¯ç¬èªã®ã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹ã䜿çšããŸãããããŠãååãšããŠOpenJDKã®ãããå Žåã¯ãåçŽã«ãç°¡åã«ããã±ãŒãžã®tzdata-javaã®å®æçãªããã±ãŒãžãããŒãžã£ãã¢ããã°ã¬ãŒãããããšãã§ãããã®åŸãOracleã®JDKã®ããããã«ããå¿ èŠããããŸãã¢ããã°ã¬ãŒããããŸã£ããæ°ããããŒãžã§ã³ã«å šäœJDKããŸãã¯å¥ã®äœ¿çšç¹å¥ãªãŠãŒãã£ãªãã£ãã€ã³ã¹ããŒã«JDKã®ã¿ã€ã ãŸãŒã³ã®ããŒã¿ããŒã¹ãæŽæ°ããŸãã
ãšããã§ãäžèšã®Joda-timeã©ã€ãã©ãªã¯tzdataã·ã¹ãã ããŒã¿ããŒã¹ãJVMããŒã¿ããŒã¹ã䜿çšããŠããŸãã-ã¯ããå éšã¿ã€ã ãŸãŒã³ããŒã¿ããŒã¹ããã1ã€ãããŸããããããåå¥ã®æ¹æ³ã§æŽæ°ããå¿ èŠããããŸãã
pythonã®å Žåãå¥åã®ã©ã€ãã©ãªãã€ã³ã¹ããŒã«ããå¿ èŠããããŸãïŒãããŠãå¿ããã«æŽæ°ããå¿ èŠããããŸãïŒã
javascriptã«ã¯ããã€ãã®ãµãŒãããŒãã£ã©ã€ãã©ãªããããŸãããå°ãªããšãGoogle Closureã§ãµããŒããããŠããããšãæ£ç¢ºã«èŠããŠããŸãã
äžè¬ã«ãç¹å®ã®ãœãããŠã§ã¢ãã¿ã€ã ãŸãŒã³ã䜿çšããŠå人çšããŒã¿ââããŒã¹ã䜿çšãããšããããŒãã¯ãå®æçã«è¡šç€ºãããŸããããšãã°ãThunderbirdé»åã¡ãŒã«ã¯ã©ã€ã¢ã³ãã®Lightningã«ã¬ã³ããŒã¢ãžã¥ãŒã«ã¯ãå人ã®sqliteããŒã¿ããŒã¹ãã¿ã€ã ãŸãŒã³ãšå ±ã«ä¿åãããããç¶æ ã®ææ°ã®å€æŽã«äŒŽãã調æŽã®ããã«ãã®ããŒã¿ããŒã¹ã«çŽæ¥ä»å ¥ããå¿ èŠããããŸãããããã§ãªããã°ããã¹ãŠã®äŒè°ã¯æééãã«åºèªããŸããã
äžè¬ã«ãéçºè ã®å€§éšåã¯ïŒç§ã®ãããªïŒåå·ç ã«æ©ãŸãããŠããããã¿ã€ã ãŸãŒã³ã«ã€ããŠèª°ãèããŠããããtzdataãã©ãããã©ãŒã ã®åºæ¬çãªé ä¿¡ã«èª°ãå«ãŸããŠããªããšæããŠããŸãïŒJVMéçºè ãé€ãïŒã
Androidã«ã€ããŠå¥ã®èšèãèšãããã§ããç°¡åã«èª¬æããŸã-Androidã¿ã€ã ãŸãŒã³ã¯èŠçã§ãããã©ãããã©ãŒã ãéçºããéã«ãå¥ã®tzdataæŽæ°ã¡ã«ããºã ã«ã€ããŠããŸãã¯äžçäžã®è°å¡ãã²ã©ãå€åã䌎ããšããäºå®ã«ã€ããŠã¯èª°ãèããŸããã§ããïŒèª°ãèããŠããã§ããããïŒãã¿ã€ã ãŸãŒã³ã®ããããŒã¿ããŒã¹ã¯ããã¡ãŒã ãŠã§ã¢ãã³ããŒãå¿ èŠãšããå Žåã«ã®ã¿å€æŽãããŸããäžéšã®ãã³ããŒã6ãæåŸã«èªåã®é»è©±ãèªèããªããªãããšãèãããšãtzdataã¯å€ãã®ããã€ã¹ã§æŽæ°ãããããšã¯ãããŸãããäžçŽãŠãŒã¶ãŒã¯ãããã€ã¹ã®çŸåšã®ã¿ã€ã ãŸãŒã³ããçŸåšã®ç¶æ³ã«å€å°å¿ããŠå¥ã®ã¿ã€ã ãŸãŒã³ã«å€æŽããŸãïŒããšãã°ããšãŒããã/ã¢ã¹ã¯ã¯ã§ã¯ãªããšãŒããã/ãã³ã¹ã¯ïŒãäžçŽãŠãŒã¶ãŒã¯ãŸã ãšãŒããã/ã¢ã¹ã¯ã¯ïŒGMT + 4ïŒã«äœãã§ãããåã«ç¢å°ãåãã ãã§ãããã®çµæããã¹ãŠã®ããã°ã©ã ã®ã€ãã³ãã®ã¿ã€ã ã¹ã¿ã³ãã¯1æéåã«å€æŽãããŸãããã¡ããããµãŒãããŒãã£ã®æŽæ°ãœãªã¥ãŒã·ã§ã³ãã«ãŒãåããã³äœ¿çšãããªãã·ã§ã³ããããŸããããã¹ãŠã®ãŠãŒã¶ãŒã«ã«ãŒãé»è©±ã匷å¶ããããšã¯ã§ããŸããã
. . , , , , , .
, , -. â . ãããŠãç§ãã¡ã¯çãç§ãã¡ã®ã«ã¬ã³ããŒãå€ãã®1ã€ã§ãããããããæããã䜿çšãããŠãããšã¯èããŠããŸããããããã ãã§ã¯ãããŸãããããã§éã¶ããšãã§ããŸãã
ããã幎
ããã幎ã¯ãéåžžã®å¹Žã®ããã«365æ¥ã§ã¯ãªãã366æ¥ã§ãã幎ã§ããããã幎ã§ã¯ã2æ-2æ29æ¥ã«1æ¥ãè¿œå ãããŸãã
ããã幎ã¯åçŽã§ãããWikipediaã§èª¬æãããŠããããšãå€æããããã®åŒ
ãããç§
ããããäœåãªç§ïŒèª¿æŽã®ç§ïŒãããã°ããã¹ãŠãããè€éã«ãªããŸããããã»ã¹ã®æ¬è³ªã¯ãå°çã絶ãããããã«æžéããŠãããåãæéã®ããŒã¯ã«æ²¿ã£ãæã«å¯Ÿãããã®äœçœ®ã絶ããå€åããŠããããšã§ããä¿®æ£ããªããšãæŒãšå€ã®æå»ãåžžã«å€ãããŸãããããé²ãããã«ãç§åŠè ã¯å°çã®äœçœ®ã远跡ããå¿ èŠãªè£æ£ãèšç®ããŠèª¿æŽèšç»ã«è¿œå ããŸããæžéããã»ã¹ã¯ç¡ç§©åºã§ãããããé·æçãªä¿®æ£èšç»ãç«ãŠãããšã¯äžå¯èœã§ããä¿®æ£ã®2çªç®ãå ¥åããå¿ èŠæ§ã®å€æã¯ãçŸåšã®ç¶æ³ã«å¿ããŠè¡ãããŸããå°çãçªç¶éã«å éããå Žåã«åããŠã調æŽã®ãã€ãã¹ã®ç§ãå ¥åããããšãçè«çã«ã¯å¯èœã§ãã
1ç§ã®èª¿æŽãååšããå ŽåãUTCã®æéã¯60ç§ã®å°æ¥ãšãšãã«æµããããšãç解ãããŸãã
23:59:58 23:59:59 23:59:60 # leap second 00:00:00 00:00:01
Unixæéã®æŠå¿µã§ã¯ã 60ç§ã®ç§ã®æŠå¿µã¯ãããŸãããããããç§ãåŠçããªããããæéã®ç·åœ¢è¡šçŸã§ãUTCã®çã®è¡šçŸã§ããããŸãããã
å°ãªããšãäœããã®æ¹æ³ã§UTCã®æéã«å¯Ÿå¿ããããæå»ã1ç§ããæ·±å€ã«æ»ãã«ã¯ããªãã¯ã䜿çšãããŸãã
23:59:58 23:59:59 23:59:59 # leap second 00:00:00 00:00:01
ããªãã¯ãè¡ããããã¡ã€ã«ã®ã¿ã€ã ãŸãŒã³ã®ããŒã¿ã«åºã¥ããŠãã«ãŒãã«ã«ãã£ãŠèªåŸçã«æ£ç¢ºãªã¿ã€ã ãµãŒãã¹ã®ããããã«ãã£ãŠããŸãã¯ã
ããã¯åŠå®çãªçµæãããããããã¯ã§ãã
- ç¿æ¥ã®23:59:00ãã00:01:00ãŸã§ã®ç§æ°ã¯120ã§ããã121ã§ããå¿ èŠããããŸã
- 1ç§é£ã¹ããšãéå»å šäœã1ç§é²ã
Javaã§ã¯ãåžžã«Unixæéã®æŠå¿µã«é¢é£ä»ããããŠããããããããç§ã®èæ ®ããããŸãããå€ãAPIã«ããæ°ããAPIã«ããJoda-timeã©ã€ãã©ãªã«ããããŸããããã®å Žåãæ å ±èªäœã¯ããããç§ã®tzdataã®å¡©åºçšåºŠã§ãããããã®æ¹æ³ã«ã¯JavaDoc java.util.DateïŒgetSecondsã¯ã仮説ã®Javaãã·ã³ãã£ãŒã«ãå€ç§60ãããã¯61ã«èšå®ããããšãã§ããããã€ãã®ãã®ããŸã ååšããªããšè¿°ã¹ã
ãŸãã第äºãèæ ®ããŠããªãããããç§ã®Javaã¯ã©ã¹ã®ç¬éã§ããããšã確èªããŠãã ããã
å€ãAPI
@Test public void testLeapSecond1() throws Exception { TimeZone tz = TimeZone.getTimeZone("UTC"); Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTimeZone(tz); calendar.set(2015, Calendar.JUNE, 30, 23, 59, 0); Date d1 = calendar.getTime(); calendar.set(2015, Calendar.JULY, 1, 0, 1, 0); Date d2 = calendar.getTime(); long elapsed = d2.getTime() - d1.getTime(); System.out.println(TimeUnit.MILLISECONDS.toSeconds(elapsed)); }
120
çµæã¯120ç§ã§ããã121ç§ã§ã¯ãããŸããã
次ã«ãæ°ããAPIã確èªããŸãã
æ°ããAPI
@Test public void testLeapSecond2() throws Exception { ZonedDateTime beforeLeap = ZonedDateTime.of(2015, 6, 30, 23, 30, 0, 0, ZoneOffset.UTC); ZonedDateTime afterLeap = ZonedDateTime.of(2015, 7, 1, 0, 30, 0, 0, ZoneOffset.UTC); long elapsed = afterLeap.toInstant().toEpochMilli() - beforeLeap.toInstant().toEpochMilli(); System.out.println(TimeUnit.MILLISECONDS.toSeconds(elapsed)); }
3600
æ£ç¢ºã«3600ç§ã3601ã§ããå¿ èŠããããŸããåžžã«
äœç§ã®èª¿æŽããã£ãããæ£ç¢ºã«ç¥ãããšãã§ããŸããæãç°¡åãªããšã¯ãwikiããŒãžã§ç¢ºèªããããšã§ãã
ä»ã®æ¹æ³ã§ç¢ºèªããŸãã調æŽã®ç§ã«é¢ããæ å ±ã¯ããã£ã¬ã¯ããª/ usr / share / zoneinfo / rightå ã®ã¿ã€ã ãŸãŒã³ã®è€è£œã«ãããŸãã
$ file /usr/share/zoneinfo/right/UTC /usr/share/zoneinfo/right/UTC: timezone data, version 2, 1 gmt time flag, 1 std time flag, 26 leap seconds, no transition times, 1 abbreviation char $ zdump -v /usr/share/zoneinfo/right/UTC | grep '59:60' | wc -l 26
ã¡ã€ã³ãã£ã¬ã¯ããª/ usr / share / zoneinfoã®ã¿ã€ã ãŸãŒã³ãã¡ã€ã«ã«ã¯ã調æŽç§ã«é¢ããæ å ±ã¯å«ãŸããŠããŸããã
$ file /usr/share/zoneinfo/UTC /usr/share/zoneinfo/UTC: timezone data, version 2, 1 gmt time flag, 1 std time flag, no leap seconds, no transition times, 1 abbreviation char $ zdump -v /usr/share/zoneinfo/UTC | grep '59:60' | wc -l 0
ã©ã®ããã«èŠããŠããåèš26ç§ã§ãã
次ã«ãUTC 1970-01-01 00:00:00 UTCãš2016-01-01 00:00:00 UTCã®éã«çµéããç§æ°ãèšç®ããŸããJavaïŒUnix-timeã«ããïŒãšãã®ä»ã®ããæ£ç¢ºãªæ¹æ³ã®2ã€ã®æ¹æ³ã§ã«ãŠã³ãããŸãã
æåã«JavaïŒ
1970-01-01 00:00:00 UTCããã³2016-01-01 00:00:00 UTC-Java
@Test public void testEpochDiff() throws Exception { ZonedDateTime s = ZonedDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); ZonedDateTime f = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); long elapsed = f.toInstant().toEpochMilli() - s.toInstant().toEpochMilli(); System.out.println(TimeUnit.MILLISECONDS.toSeconds(elapsed)); }
1451606400
1451606400ãå€æããå確èªããŸããã
$ dateutils.ddiff --from-zone "right/UTC" -f '%S' "1970-01-01 00:00:00" "2016-01-01 00:00:00" 1451606400
ãã¹ãŠãåæããŸã-1451606400ç§ãä»ã§ã¯é«ç²ŸåºŠã®æŠåšãèšå®ããŸãïŒ
$ dateutils.ddiff --from-zone "right/UTC" -f '%rS' "1970-01-01 00:00:00" "2016-01-01 00:00:00" 1451606425
ããã§ãçŸåšã¯1451606425ç§ã§ããéãã26ç§ã§ã¯ãªã25ç§ã§ããçç±ã¯æ確ã§ã¯ãããŸããããä»ã®æ£ç¢ºãªèšç®æ©ã¯ãŸã èŠã€ãããŸããã
å°ãªããšãæšæºJavaã䜿çšããŠå¶åŸ¡ãšã³ãžã³ãäœæããå Žåãã¿ã€ã ãã©ãã«ã«ã¯æ·±å»ãªåé¡ãããããšãæããã«ãªããŸããæ£ç¢ºãªæéãèšå®ãããšãããŒã«ããã¯ããå¿ èŠãããç§æ°ãæ£ç¢ºã«èšç®ã§ããªããªããŸãããšã©ãŒã¯æ倧ã§30åã«ãªããŸããæ£ç¢ºãªæªæ¥ãžã®ç§»åã¯ãååãšããŠãŸã£ããäžå¯èœã§ã-調æŽã®ç§æ°ãäºåã«äºåã«æ±ºå®ã§ããªãããã§ãã
currentTimeMillisïŒïŒãnanoTimeïŒïŒ
Javaã®ä»ã®ã»ãšãã©ã®ãã©ãããã©ãŒã ãšåæ§ã«ãæéã®ãœãŒã¹ã¯2ã€ãããŸãã1ã€ç®ã¯å ±éã®æé軞ã«çŸåšã®ããŒã¯ã衚瀺ãã2ã€ç®ã¯ããã»ããµã«é»åãäŸçµŠãããŠããã®æéãã«ãŠã³ãããŸãã
- java.lang.SystemïŒcurrentTimeMillis-1970幎1æ1æ¥ã®åå0æïŒUTCã¿ã€ã ãŸãŒã³ïŒããçµéããããªç§æ°ãè¿ããŸãããã®ã¡ãœããã¯ãjava.util.Dateããã³java.util.Calendarã®æ°ããã€ã³ã¹ã¿ã³ã¹ãäœæãããšãã«äœ¿çšãããŸããæ»ãå€ã¯ããªç§åäœã§æž¬å®ããããšããäºå®ã«ãããããããå®éã®ç²åºŠã¯ã¯ããã«é«ããªãå¯èœæ§ããããŸã-æ°åããªç§ãŸã§ãå調æ§ã®ä¿èšŒã¯ãããŸãããå€ã¯ããªãã¬ãŒã¿ãŒãŸãã¯æ£ç¢ºãªã¿ã€ã ãµãŒãã¹ã«ããã·ã¹ãã ã¯ããã¯ã®è»¢éã®çµæãšããŠãååŸã«ãžã£ã³ãããå¯èœæ§ããããŸãã
- java.lang.SystemïŒnanoTime-ããç§å»ã¿ã®æœè±¡çãªæ°ãè¿ããŸãããã£ãã¯ã®å€ã¯ãå¿ ãããããã»ããµã«ãŠã³ã¿ãŒããååŸããããšã¯éããŸããããŸããææ°ã®ããŒããŠã§ã¢ã«ã¯ãä»ã®å€ãã®æ£ç¢ºãªä¿¡å·æºããããŸãã第äºã«ããã«ãããã»ããµã·ã¹ãã ã§ã¯ãåããã»ããµã«ç¬èªã®ã«ãŠã³ã¿ããããããé£ç¶ããã¡ãœããåŒã³åºããç°ãªãã¹ã¬ããã§ãžã£ã³ãå€ãè¿ãå¯èœæ§ããããšããåé¡ããããŸããç¹å®ã®å®è£ ã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ããŒããŠã§ã¢ãã¿ã€ããããã³ããŒãžã§ã³ã«ãã£ãŠç°ãªããŸãããŸããçµæã®ããç§ã®ç²ŸåºŠã«ãããããããå®éã®ç²åºŠã«ã€ããŠã¯ä¿èšŒãããŸãã-粟床ã¯æ°åããªç§ãŸã§äœäžããå¯èœæ§ããããŸãããjava.lang.Systemã®ç²åºŠãããæªããããŸããïŒcurrentTimeMillis
ãããã®è³ªåããã³ãã®ä»ã®è³ªåã«ã€ããŠã¯ãããã§è©³ãã説æããŸãã
- Hotspot VMã®å éšïŒã¯ããã¯ãã¿ã€ããŒãããã³ã¹ã±ãžã¥ãŒãªã³ã°ã€ãã³ã
- System.nanoTimeïŒïŒã¯ãŸã£ãã圹ã«ç«ã¡ãŸãããïŒ
- clock_gettimeïŒCLOCK_REALTIMEïŒã«é¢ããLinux FAQ
åäœæé枬å®
åã®ç« ã«åºã¥ããŠãæéã®æ£ãã枬å®ã«ã€ããŠãã£ã1ã€ã®çµè«ãåºãããšãã§ããããã«æãããŸã-java.lang.SystemïŒnanoTimeã¡ãœããã®ã¿ã䜿çšããå¿ èŠããããŸããjava.lang.SystemïŒcurrentTimeMillisã¡ãœããã¯ãã·ã¹ãã æéãå€æŽããããšå€ãã¹ããããããå¯èœæ§ããããããé©åã§ã¯ãããŸããããã®çµè«ã¯ãäž¡æ¹ã®ã¡ãœããã®JavaDocãèªãããšã§ã確èªã§ããŸãã
ãã ããjava.lang.Threadã¯ã©ã¹ã®ã¡ãœãããèŠããšãéåžžã«å¥åŠãªããšãããããŸãã
java.lang.ThreadïŒjoinïŒlongïŒ
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }
ããšãã°ãjava.util.concurrent.ExecutorServiceã§ã¯System.nanoTimeããã§ã«äœ¿çšãããŠãããããããã¯éåžžã«å€ããã®ã§ãã
åã質åã¯ãããŸããŸãªãµãŒãããŒãã£ã©ã€ãã©ãªã«ãéåžžã«é¢é£ããŠãããSystem.currentTimeMillisïŒïŒã«åºã¥ãæéã®æž¬å®å€ãèªå·±èšè¿°ã³ãŒãã«å®è£ ãããŠãããã©ããã¯åçŽã«æ°ããããŸããã
è©Šéšæé
ãã®ä»¶ã«é¢ããŠã¯ãç§ã¯æšã«åºããŸãã-åé ã§è¿°ã¹ã8幎ã®ãããžã§ã¯ãã§æåããçµéšã«ã€ããŠã話ãããŸãã
ãããžã§ã¯ãã¯éåžžã«è²¬ä»»ããããŸãã-ãéããã€ã³ããè€éãªããžãã¹ããžãã¯ãããŒã«ããã¯ãã¿ã€ã ã¢ãŠãæäœãæéçµéåŸã®ã¹ããŒã¿ã¹ã®å€æŽãªã©ã
ç§ãšããã³ããšã³ãã®2人ã®éçºè ãããŸãããããã¹ã¿ãŒã¯ãŸã£ããããŸããã§ããããµãŒããŒã«ã¯ãIoCãã€ã³ãžã§ã¯ã·ã§ã³ãäœçµåãé«åéã®ååã®ãã¬ãŒã ã¯ãŒã¯ããããŸããããããéçºã«å¿ èŠãªãã®ã«ãªããšããããã§ã¯ãããŸãããããããé¢é£ããæ¬ãèªã¿å§ãããããããã®ãé£ãããªããŸããå¿é ããã®ã¯ãã¹ãã ãã§ããããã¹ããæžãå§ããéçºè ã»ã©ç¡åã§ç¡è²¬ä»»ã§çãããããŠãããã®ã¯ãããŸãããããããé ããæ©ããããã®ãŽãã«é²ãããšãç¥ã£ãŠããŸããã
äžè¬çã«ãç§ã¯ããã«æ±ºå®çã«ã€ã³ã¿ãŒãã§ãŒã¹ãéå§ããŸããïŒ
org.myproject.Chronometer
public interface Chronometer { Date getCurrentMoment(); long getCurrentMs(); long getCurrentTicks(); }
ãã®ã€ã³ã¿ãŒãã§ãŒã¹ã¯ãçŸåšã®æéãå¿ èŠãªã»ãŒãã¹ãŠã®å Žæã«æ³šå ¥ãããŸããæé远跡ã¯ã©ã¹ïŒorg.myproject.TimerïŒã¯ããã®ã€ã³ã¿ãŒãã§ã€ã¹ãã³ã³ã¹ãã©ã¯ã¿ãŒã«ååŸããŸãã
ãã®ã¢ãããŒãã§æãé£ããã®ã¯ãäœãå¯èœããäœãäžå¯èœããæãåºãããšã§ãã
ããã¯å¯èœã§ãããäžå¯èœã§ã
# user.setCreated(new Date()); user.setModified(new Date()); # Date now = chronometer.getCurrentMoment(); user.setCreated(now); user.setModified(now); # Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, -3); Date expiration = calendar.getTime(); # Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(this.operationTimeZone) calendar.setTime(chronometer.getCurrentMoment()); calendar.add(Calendar.DAY_OF_YEAR, -3); Date expiration = calendar.getTime();
ãã¡ãããå人ã®ã³ãŒãã§ã®ã¿ãã®ãããªéšåãå®å šã«ãã¬ãŒã¹ã§ããŸãããæ°žç¶æ§ãšããžãã¹ããžãã¯ãæ åœããã³ãŒãã«ãã®æ£ããã¢ãããŒããå®è£ ããããšãéèŠã§ãã
ãã¹ãã®åé¡ãè°è«ãããšããããŒã¿ããŒã¹ãå¿ããªãããšã¯äžå¯èœã§ãã
å»æ¢ããããšã³ããªãåé€ãã
# - DELETE FROM records WHERE created <= DATE_SUB(NOW(), INTERVAL 30 DAY) # - DELETE FROM records WHERE created <= DATE_SUB(:now, INTERVAL 30 DAY) # - / DELETE FROM records WHERE created <= :expiration
ãã®ã¢ãããŒãã§ã¯ãã©ãã§ãæ£ããå®è£ ãããŠããã°ããã¹ãŠã®èªåãã¹ãããã€ã§ãéä¿¡ã§ããŸã-ãã¹ãŠã®ã³ãŒãã¯1953幎ãŸãã¯2312幎ã«ç°¡åã«å®è¡ã§ããŸãã
è©ŠéšäŸ
// "" mockChronometer.setCurrentTime("2120.06.10 15:33:11"); // "" Period period = new Period(); period.setStart(TimeUtils.parse("2120.06.01 00:00:00")); period.setEnd(TimeUtils.parse("2120.07.01 00:00:00")); period.setIndex("wdwwddwwdw"); period.setType(PeriodType.MONTH); period.setDescription("efefef"); periodsDao.save(period); // - - "2120.06.10 15:33:11". // , . // - ( ). checkSomething1(); // ... 2 mockChronometer.setCurrentTime("2120.06.12 15:38:14"); // checkSomething2();
MockChronometerãšTimeUtilsã®åŒã³åºãã§ã¿ã€ã ãŸãŒã³ãèšå®ããªãããšã«ã€ããŠæ¢ã«ã¢ãã¿ãŒã«æãåããŠkg / amãæããŠãããå Žåã¯ã次ã®ç¹ã«æ³šæããŠãã ãããçŸåšã®ã¿ã€ã ãŸãŒã³ã®å®¹èªã§ãã¹ãã«ããããæ®ãããšã¯ããã¹ããè匱ã«ããããšãæå³ããŸããå®éãã¡ãœããåŒæ°ã§ãŸãŒã³ãç¹ã«æå®ãããŠããªãå Žåãããã©ã«ãã§ã¯äž¡æ¹ã®ã¯ã©ã¹ãããã©ã«ãã§UTCã¿ã€ã ãŸãŒã³ã§åäœããŸãã
Java 8ã§ã¯ãæ°ããDate Time APIãjava.time.Clockã€ã³ã¿ãŒãã§ãŒã¹ãå°å ¥ããŸãããããã¯ãŸã£ããåãç®çã®ããã«å°å ¥ãããŸããããäžè¬ã®äººããã§ã«ãããé«ãè©äŸ¡ããŠãããã©ããã¯ããããŸããã
å¥ã®ã¢ãããŒãã¯ããšãŒãžã§ã³ããæå®ããŠå¥ã®JVMã§ãã¹ããå®è¡ããããšã§ããSystem.nanoTimeïŒïŒããã³System.currentTimeMillisïŒïŒãžã®åŒã³åºããã€ã³ã¿ãŒã»ããããããã®ã³ãŒãã€ã³ã¹ãã«ã¡ã³ããŒã·ã§ã³ãçæããŸããç§ã¯ãã®ã¢ãããŒããè©ŠããŠããªãã®ã§ãæ¢è£œã®ãœãªã¥ãŒã·ã§ã³ã®å€§ãŸããªæ€çŽ¢ã§ã¯ æäŸãããŸãããããå ç¢ãªãªãã·ã§ã³ã¯ããã«ãããã»ã¹äžã®ãœãŒã¹ã³ãŒãã®åçŽãªååŠçã®ããã§ããSystem.nanoTimeïŒïŒãSystem.currentTimeMillisïŒïŒãnew DateïŒïŒãCalendar.getInstanceïŒïŒãžã®åŒã³åºãããã®ã¯ã©ã¹ãžã®åŒã³åºãã«çœ®ãæããŸãã
æ¥ã®ãã¬ãŒã ã¯ãŒã¯
8幎åã®æ¥ã«ã¯LocaleResolverããããŸããããTimezoneResolverã¯ãããŸããã§ããïŒç§ã®æèŠã§ã¯ãåé¡ã«å¯Ÿããäžè¬çãªæ 床ãå®å šã«ç¹åŸŽã¥ããŠããŸãïŒãç¬èªã®ããããäœæãããšåæã«ãDispatcherServletã®ãµãã¯ã©ã¹ãäœæããå¿ èŠããããŸããã
éåžžã«å€æ°ã§ã¯ãªããã³ãã¥ããã£ïŒç§ã®ãã®ãå«ãïŒããã®æ°žç¶çãªèŠæ±ã®åŸãèŠæ±ã®ã¿ã€ã ãŸãŒã³ã®ã¹ã¿ãããªãŸã«ãããã¬ãŒã ã¯ãŒã¯ã®ç¬¬4ããŒãžã§ã³ã§å°å ¥ãããŸããã
MVC
å¥ã®è³ªåã¯ãã³ã³ãããŒã©ãŒã®ã¿ã€ã ãŸãŒã³ãç¥ã£ãŠããã³ãã¬ãŒããšã³ãžã³ã«æ£ããèšå®ããæ¹æ³ã§ãã
FreeMarkerã«ã¯ãçŸåšã®ã¬ã³ããªã³ã°çšã®ç¹å¥ãªèšå®ããããŸãã
<#setting time_zone="Europe/Moscow">
JSPã®å Žåãåäžã®åºåã«å¯ŸããŠã¿ã€ã ãŸãŒã³ãåå¥ã«æå®ããããšãããããã¯å šäœã«å¯ŸããŠçŽã¡ã«ã¿ã€ã ãŸãŒã³ãæå®ããããšãã§ããŸãã
<fmt:formatDate type="both" value="${now}" timeZone="Europe/Moscow"/> <fmt:timeZone value="Europe/Moscow"> <fmt:formatDate type="both" value="${now}"/> </fmt:timeZone>
Velocity ã«ãäœããããŸãããç§ã¯å人çã«ã¯è©ŠããŠããŸããã
ããŒã¿ããŒã¹ã«æéãä¿åãã
ããŒã¿ããŒã¹ã«æå»ãã€ã³ããæ ŒçŽããæã匷åãªæ¹æ³ã¯ãå€java.util.DateïŒgetTimeïŒïŒãåçŽãªæ°å€ã®é·ãUnixæéå€ãšããŠããŒã¿ããŒã¹ã«æž¡ãããšã§ãããããã£ãŠãèªã¿åãæã«ãã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããŠlongãjava.util.Dateã«å€æããŸããããã¯ãHibernateãŸãã¯RowMapperã³ã³ããŒã¿ãŒã§å®è¡ã§ããŸãããã®å Žåã®ããŒã¿ããŒã¹ã¯æéã«ã€ããŠäœãç¥ããªããããçªç¶ã®åœ±é¿ã¯ãããŸãããã©ãã«ã®åœ¢åŒã§äžæãã€ã³ãã衚瀺ããå¿ èŠãããå Žåãããšãã°MySQLã§ãFROM_UNIXTIMEã¡ãœããããã€ã§ãåŒã³åºãããšãã§ããŸãã
ãã®ã¡ãœããã¯ãããŒã¿ããŒã¹ããã³/ãŸãã¯ã¹ãã¢ãããã·ãŒãžã£ã®ã¯ãšãªã«æéã®ããæäœããªãå Žåã«é©ããŠããŸãããã®ãããªæäœãããå ŽåïŒéçºãç°¡åã«ããããã«ããã¡ããåé¿ããæ¹ãããã§ãããïŒãã¿ã€ã ãŸãŒã³ãªãã§ããããééã§ããªãããšãæ¢ã«èªèããŠããŸãããã®å Žåãå€æãŸãã¯å ¥åºåæäœäžã«ã©ã®ã¿ã€ã ãŸãŒã³ãæ©èœããããæ£ç¢ºã«ç解ããå¿ èŠããããŸãã
- ãµãŒããŒã«ããã©ã«ãã§æå®ãããŠããã¿ã€ã ãŸãŒã³ã
- DBMSã®ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ã
- ããŒã¿ããŒã¹ã®ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ã
- ããŒãã«ã«ããã©ã«ãã§æå®ãããŠããã¿ã€ã ãŸãŒã³ã
- æ¥ç¶ã®ããã©ã«ãã®ã¿ã€ã ãŸãŒã³ã
- ã¿ã€ã ã¹ã¿ã³ããšãšãã«ã»ã«ã«ä¿åãããã¿ã€ã ãŸãŒã³ã
ã©ããªå Žåã§ãããã®ããã«ç¹å¥ã«èšèšãããåã§æéãä¿åããå¿ èŠã¯ãªããšèšãããã§ããçŽæ¥ã¹ãã¬ãŒãžãé·ã䜿çšãããªãã·ã§ã³ã¯ãã©ããããããç Žãã®ãéåžžã«é£ããïŒæ¹æ³ãããããªãïŒããã®æ¹æ³ã¯ã¹ãã¬ãŒãžã®ã¿ã€ãã«äŸåããªããšããã ãã§ãã
ãã®ããã«ç¹å¥ã«èšèšãããã¿ã€ãã§æéãä¿åããå ŽåãããŒã¿ããŒã¹ãšãã®ãã©ã€ããŒã®æå®ãããèšå®ã§ãå€åºå ã§ã¿ã€ã ãŸãŒã³ãå€æŽããå Žåã§ããæå®ãããæéå€ãäžè²«ããŠãã®ããŒã¿ããŒã¹ã®ãã€ãã£ã管çã³ã³ãœãŒã«ã«æžã蟌ãŸããèªã¿åããã衚瀺ãããããšã確èªããå¿ èŠããããŸãã
ççŸã¯ãããŒã¿ããŒã¹ããã€ã³ãã§ã¯ãªãã¿ã€ã ã¹ã¿ã³ããå®éã«ä¿åã§ãããšããäºå®ã«ãã£ãŠåŒãèµ·ããããå¯èœæ§ããããŸããããšãã°ãMySQLã®å Žåãæéãä¿åããããã®2ã€ã®æšæºã¿ã€ãããããŸãïŒTIMESTAMPãšDATETIMEã
å ¬åŒææžã«ãããšãTIMESTAMPã¯æ£ç¢ºãªæç¹ãä¿åããã¢ã¹ã¯ã¯ã®ã¿ã€ã ãŸãŒã³ã§ã¯ã©ã€ã¢ã³ãããå€ã2015-01-01 12:00:00 MSKããåä¿¡ãããšãå¥ã®å Žåã¯ã2015-01-01 09:00:00 UTCããè¿ããŸãã UTCã¿ã€ã ãŸãŒã³ã®ã¯ã©ã€ã¢ã³ããããã¯1ã€ã®æç¹ã«å¯Ÿå¿ããæ¬è³ªçã«å®å šã«æ£ãããã®ã§ãããŸããDATETIMEã¿ã€ãã§ã¯ãMSKã¯ã©ã€ã¢ã³ãããã2015-01-01 12:00:00 MSKããåä¿¡ãããšãMySQLãµãŒããŒã¯ã2015-01-01 12:00:00 UTCããUTCã¯ã©ã€ã¢ã³ãã«è¿ããŸããããã¯å¥ã®æç¹ã«å¯Ÿå¿ããŸããã以éã®èšç®ã¯ãã¹ãŠäžæ£ç¢ºã«ãªããŸãã
MySQLã確èªããŠãã ããããŸãããã¹ãŠãæºåããŸãã
DBã®èµ·å
$ sudo docker run --name mysql-time -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server:5.7 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 82bb3eebc8bc mysql/mysql-server:5.7 /entrypoint.sh mysq 5 minutes ago Up 5 minutes 3306/tcp mysql-time $ sudo docker exec -it 82bb3eebc8bc bash [root@82bb3eebc8bc /]# TZ=Europe/Moscow mysql -u root -p Enter password: mysql> CREATE DATABASE test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> CREATE TABLE dates (id INTEGER, t1 TIMESTAMP, t2 DATETIME); Query OK, 0 rows affected (0.02 sec)
ã»ãã·ã§ã³ã§ããšãŒããã/ã¢ã¹ã¯ã¯ããŸãŒã³ãèšå®ããã¬ã³ãŒããäœæããŸãã
ãšãŒããã/ã¢ã¹ã¯ã¯
mysql> SET time_zone = 'Europe/Moscow'; Query OK, 0 rows affected (0.00 sec) mysql> SELECT @@session.time_zone; +---------------------+ | @@session.time_zone | +---------------------+ | Europe/Moscow | +---------------------+ 1 row in set (0.00 sec) mysql> INSERT INTO dates VALUES (1, '2015-01-01 12:00:00', '2015-01-01 12:00:00'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM dates WHERE id = 1; +------+---------------------+---------------------+ | id | t1 | t2 | +------+---------------------+---------------------+ | 1 | 2015-01-01 12:00:00 | 2015-01-01 12:00:00 | +------+---------------------+---------------------+ 1 row in set (0.00 sec)
ã»ãã·ã§ã³ã®ã¿ã€ã ãŸãŒã³ããUTCãã«å€æŽããã¬ã³ãŒããå床èªã¿åããŸãã
UTC
mysql> SET time_zone = 'UTC'; Query OK, 0 rows affected (0.00 sec) mysql> SELECT @@session.time_zone; +---------------------+ | @@session.time_zone | +---------------------+ | UTC | +---------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM dates WHERE id = 1; +------+---------------------+---------------------+ | id | t1 | t2 | +------+---------------------+---------------------+ | 1 | 2015-01-01 09:00:00 | 2015-01-01 12:00:00 | +------+---------------------+---------------------+ 1 row in set (0.00 sec)
ãã£ãŒã«ãt1ã®ã¿ã€ã ã¹ã¿ã³ããå€æŽãããŠããããšãããããŸãããã¿ã€ã ãŸãŒã³ãå€æŽããŠããã£ãŒã«ãt2ã®ã¿ã€ã ã¹ã¿ã³ãã¯å€æŽãããŠããããæ°å€è»žäžã®å¥ã®ãã€ã³ãã«å¯Ÿå¿ããŠããŸãã
ã»ãšãã©ã®ããŒã¿ããŒã¹ã«ã¯ãæ¥ä»ãšæå»ã®ã¿ãä¿åããç¹å®ã®ã¿ã€ãããããŸãããã®ãããªåã¯ãã¿ã€ã ãŸãŒã³ïŒæ¢ã«ç¥ã£ãŠããïŒãæå®ããã«æç¹ã«ç§»åããããšã¯ã§ããŸãããããããã®å€ãæ¬ èœéšåïŒããããæå»ãŸãã¯æ¥ä»ïŒã§è£ãå¿ èŠããããŸããå®éãããšãã°ãæŽå²çåºæ¥äºã®æ¥ã®åçŽãªçµè«ã®å Žåãªã©ããããå¿ èŠã§ãªãå Žåããã®ãããªã¿ã€ãã¯æç¹ã«æã¡èŸŒãŸããªããããããŸãããããã¯ã©ãã«ïŒãŸãã¯ãã®äžéšïŒã§ããã軞äžã®äžæçãªãã€ã³ãã§ã¯ãªãããšãèŠããŠããå¿ èŠããããŸãã
å€ãã®ããŒã¿ããŒã¹ã«ã¯ãã¿ã€ã ãã€ã³ãèªäœã«å ããŠãå ¥åãããã¿ã€ã ãŸãŒã³ã«é¢ããè¿œå æ å ±ãä¿åããããŸããŸãªã¿ã€ãããããŸããããšãã°ããã®å€ã®äž»ãªã¿ã€ã ãŸãŒã³ãç¹å®ããå¿ èŠãããå Žåã«åœ¹ç«ã¡ãŸãã
NTP
ã·ã¹ãã ã®çŸå°æéãå®éã®æå»ãšç°ãªãçç±ã«é¢ä¿ãªããããŸããŸãªãããã³ã«ã䜿çšããæ£ç¢ºãªæå»é ä¿¡ãµãŒãã¹ãå©ãã«ãªããŸããæãäžè¬çãªã®ã¯NTPãããã³ã«ã§ãã
ååæèšã¯éåžžã«é«äŸ¡ãªã®ã§ããããã«é¢é£ä»ãããããµãŒããŒã«è² è·ããããããªãããã«ãNTP ã¯éåžžã®ãŠãŒã¶ãŒã®èŠæ±ãåŠçããããã«éå±€å šäœãæ§ç¯ããŸãã
ã¯ã©ã€ã¢ã³ãã¯ãç°ãªããµããããã§äžåºŠã«è€æ°ã®æ£ç¢ºãªã¿ã€ã ãµãŒãã¹ãå®æçã«ããŒãªã³ã°ãã UDPããŒã¿ã°ã©ã ã®éåä¿¡ã«è²»ãããæéãè£æ£ããå€ãå€ã§ããå€ãç Žæ£ããŠãããç¹å¥ãªã¢ã«ãŽãªãºã ã§ããããå¹³ååããŸãã
ç³ãç«ãŠæžã¿ ãããªãã¯ãããã¯ãŒã¯ã§äœ¿çšãããšãå¯èœãªåæ粟床ã¯10ããªç§ã«éããå¯èœæ§ããããŸãã
éç¥
ã»ãšãã©ã®ãµãŒãã¹ã§ã¯ãã¢ãã€ã«éç¥ïŒããã·ã¥ãšSMSïŒã¯ãç·æ¥ïŒã¡ãã»ãŒãžãæ°ããåæ ãæ¯æãïŒãšéç·æ¥ïŒããã¢ãŒã·ã§ã³ãåºåããªãã¡ãŒãã«ãŒã«ã®å€æŽïŒã®2ã€ã®ã¯ã©ã¹ã«åé¡ã§ããŸãããŠãŒã¶ãŒã¯ãã€ãã³ããçºçãããšããã«æåã®ãã®ãåä¿¡ããããšãæåŸ ããå¯èœæ§ãé«ãã2çªç®ã®å Žåãã€ãã³ãèªäœã¯ããã§ã¯ãªãå¯èœæ§ãããããŠãŒã¶ãŒã«ãšã£ãŠéœåã®è¯ãæéã«ãŠãŒã¶ãŒãéªéããæ¹ãè¯ãã§ããããããšãã°ãåå10æããååŸ20æãŸã§ã®ãããªãããã©ã«ãã®å¿«é©ãªæéãèæ ®ããããšãã§ããŸãããããéèŠãªå Žåã¯ãå¿«é©ãªæéã確ä¿ããŠãåå¥ã«æåã§æå®ã§ããŸãã
äœããã®æ¹æ³ã§ããã®æéãããã¿ã€ã ãŸãŒã³ã§è§£éãã矩åããããããããŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ã«ãªãããšã¯æããã§ããååãšããŠããµãŒãã¹ãç¹å®ã®å°åïŒããšãã°ã1ã€ã®éœåžïŒã§ã®ã¿æ©èœããå Žåãããã¯ãã¹ãŠç¡èŠã§ãããã¹ãŠã®ãŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ããµãŒããŒã®ã¿ã€ã ãŸãŒã³ãšäžèŽãããšæ³å®ã§ããŸãããããã¯åæ£ãµãŒãã¹ã«ã¯ååã§ã¯ãããŸããã
ãããã£ãŠãã¿ã€ã ãŸãŒã³ã¯ããã¯ã°ã©ãŠã³ãã¿ã¹ã¯çšã«ä¿åããå¿ èŠããããŸã-å°ãªããšããŠãŒã¶ãŒã«å¯ŸããŠåå¥ã«ãããã«ã¯ãŠãŒã¶ãŒããã€ã¹ããšã«åå¥ã«ä¿åããå¿ èŠããããŸããçŸä»£ã®çŸå®ã§ã¯ãåãã¢ããªã±ãŒã·ã§ã³ã¢ã«ãŠã³ããè€æ°ã®ããã€ã¹ã§äžåºŠã«äœ¿çšã§ããŸããããšãã°ãã¢ã¹ã¯ã¯ã®èªå® ã«åžžé§ããŠããæ®ã眮ãåã®Android TVãªã©ã§ãããŠãŒã¶ãŒãäŒæäžã«ã¿ã€ã§é¢ããAndroidã¿ãã¬ããäžããããã£ãŠãäž¡æ¹ã®ããã€ã¹ã®éç¥ãç°ãªãæéã«éä¿¡ããå¿ èŠãããå¯èœæ§ããããŸãããŠãŒã¶ãŒã®ã¿ã€ã ãŸãŒã³ãšãŠãŒã¶ãŒã«ãšã£ãŠå¿«é©ãªæéãããããããã€ã§ãã¹ãã ãéå§ã§ããæé軞äžã®ãã€ã³ããèšç®ã§ããŸãã
äŸ
Date now = chronometer.getCurrentMoment(); TimeZone timeZone = userDeviceRecord.getTimeZone(); Calendar calendarFrom = Calendar.getInstance(timeZone); calendarFrom.setTime(now); calendarFrom.set(Calendar.HOUR_OF_DAY, comfortHourFrom); Calendar calendarTill = Calendar.getInstance(timeZone); calendarTill.setTime(now); calendarTill.set(Calendar.HOUR_OF_DAY, comfortourHourTill); if (now.after(calendarTill.getTime())) { calendarFrom.add(Calendar.DAY_OF_YEAR, +1); } long delayMs = Math.max(calendarFrom.getTime().getTime() - now.getTime(), 0); notificationService.sendNotificationWithDelay(msg, delayMs);
åå/ååŸ
ã¿ã€ã ã¹ã¿ã³ãã®12æéã®ãã¬ãŒã³ããŒã·ã§ã³ã«ã€ããŠè©±ããŠããããšããŸã ç解ããŠããªã人ã¯ããã«ããªããšæããŸãã
ãããã誰ãããããã®ç¥èªã®ç¿»èš³æ¹æ³ãç¥ã£ãŠããããã§ã¯ãããŸããïŒamïŒlatãAnte meridiem-æåéããæ£ååãïŒããã³pmïŒlatãPost meridiemæåéã-ãæ£ååŸãïŒã
å€ãã®äººã«ãšã£ãŠé©ãã¯ãçå€äžãååŸ12æãŸãã¯åå0æã§ã¯ãªããéåžžã«åå12æã§ããããšã§ããåæ§ã«ãæ£åã¯åå12æã§ãååŸ0æã§ããããŸããããååŸ12æã§ãããã®ãããªæå®ã§ã¯ããã®åœ¢åŒã«æ £ããŠããåœã®å± äœè ã§ããæ··ä¹±ããŠããã®ã§ãããŸããŸãªããªãã¯ãæãã€ããŸãã
ãªã¢ã«ã¿ã€ã ã¯ããã¯
ããããã¢ãŒããã¯ãã£ã®ããŒããŠã§ã¢ã«ã¯ãããããªãŒãæ¥ç¶ãããç¹å¥ãªãããããããŸããããã¯ãªã¢ã«ã¿ã€ã ã¯ããã¯ãŸãã¯Linuxçšèªã§ãããŒããŠã§ã¢ã¯ããã¯ãã§ãã
å€éšæ¡ä»¶ã«é¢ä¿ãªããé»åã§éãã«ã«ãã«ãé³ãããŠãŸãïŒã¡ã€ã³é»æºãã·ã¹ãã ãŠãããã«äŸçµŠãããŠãããã©ããããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ãã€ã³ã¹ããŒã«ãããŠãããã©ãããããã³ã¢ã¯ã·ã§ã³ã®æç¡ãããããªãŒã®ãšãã«ã®ãŒããªããªããšããããã®é²è¡ã¯äžæãããŸããããã¯ãå®éã«æ©åšã§ååã«æ©ãèµ·ãããŸãã
Linuxã§ïŒå®éã«ãã®ãããã«ã³ãã³ããéä¿¡ããããã«ïŒããŒããŠã§ã¢ã¯ããã¯ã管çããã«ã¯ãç¹å¥ãªhwclockã³ãã³ãããããŸããæ©åšïŒéåžžã¯/ dev / rtcïŒãšçŽæ¥éä¿¡ãããããrootæš©éã§å®è¡ããå¿ èŠããããŸãã
$ sudo hwclock Wed 30 Dec 2015 17:59:12 MSK -0.328637 seconds
ãã®ã³ãã³ãã¯ãããŒããŠã§ã¢ã¯ããã¯ããåä¿¡ããæå»ãã·ã¹ãã ã®ã¿ã€ã ãŸãŒã³ã«èªåçã«ç§»åããŸããããããåºåã®æåŸã«ãã®å¥åŠãªãã€ã¢ã¹ã¯äœã§ããïŒ
å®éã®ãšãããLinuxã«ãŒãã«ã«ã¯ãä»ã«ãç¬èªã®ã¯ããã¯ïŒã·ã¹ãã æéïŒããããŸãã Linuxã«ãŒãã«ã¯ãã«ãŒãã«ããŒãã®éå§æã«ãããããèªã¿åãå€ã1åèªã¿åãããã®åŸãå²ã蟌ã¿äžã«èªåçã«ãã§ãã¯ããŸããã»ãšãã©ã®å Žåãããã¯1ã€ã®åçŽãªçç±ã§è¡ãããŸãããåãŠãŒã¶ãŒãªã¯ãšã¹ãã«å¯ŸããŠã·ãªã¢ã«ãã¹äžã®ããŒããŠã§ã¢ã¯ããã¯ãããããããŒã¿ãæ¯èŒçé·ãæéèªã¿åããããããŒã«ã«ã®ãœãããŠã§ã¢ã«ãŠã³ã¿ãŒã®ã¿ãä¿æããæ¹ãç°¡åã§ãã Linuxã«ãŒãã«ãããŒããŠã§ã¢ã¯ããã¯ããå€ãèªã¿åã£ãåŸãçè«çã«ã¯ãåŸè ã§ä»»æã®æäœãå®è¡ã§ããŸããããã¯ãã¢ããªã±ãŒã·ã§ã³ã®æéã«åœ±é¿ããŸããã
ãããã£ãŠãã·ã¹ãã ã§ã¯2æéã䞊è¡ããŠå®è¡ãããŸããã³ã¢ã®å®éã®é»åæèšãšãœãããŠã§ã¢æèšã§ãã hwclockã³ãã³ãã®åºåã«è¡šç€ºãããéãã¯å¿ ç¶çã«
ãããŸããããã®åé¡ã解決ããã«ã¯2ã€ã®ãªãã·ã§ã³ããããŸãïŒäžéšã®åé¡ã§ã¯ãŸã£ããåé¡ãããŸããïŒã
ã·ã¹ãã ããããã¯ãŒã¯ã«æ¥ç¶ãããŠããªããšä»®å®ããŠãæåã®ãªãã·ã§ã³ãæ€èšããŠãã ãããããŒããŠã§ã¢ã¯ããã¯Iron Clockã¯äžæããããšãªã24æéåäœãããããå¹³åããŠæ¯æ¥åãéã§ééããããŸãã hwclockãŠãŒãã£ãªãã£ã¯ããããããå€ãèªã¿åããšãã«ãç¹å¥ãªä¿®æ£ã¡ã«ããºã ã«ãããããå¹³æºåã§ããŸãããŠãŒã¶ãŒãããŒããŠã§ã¢ã¯ããã¯ã2åèšå®ããã°ãèšå®ã®ééãããªãé·ããªããŸãã hwclockãŠãŒãã£ãªãã£èªäœã¯ãæ¥äžã«ããŒããŠã§ã¢ã¯ããã¯ãã©ãã ãééããããèšç®ãããã®åŸããã®å€ã/ etc / adjtimeãã¡ã€ã«ã«ä¿åããŸãããã®åŸãããŒããŠã§ã¢ã¯ããã¯ããå®æçã«å€ãèªã¿åããã·ã¹ãã æéå€ãèšå®ã§ããŸãããŠãŒãã£ãªãã£èªäœã¯ã以åã«ä¿åãããæ¥æ¬¡ãšã©ãŒå€ã«ãã£ãŠçŽ¯ç©ãšã©ãŒãä¿®æ£ããŸãã
2çªç®ã®ãªãã·ã§ã³ã¯ãã«ãŒãã«ã«System Timeãå®æçã«æ£ããã€ã³ã¹ããŒã«ããäœããã®æ¹æ³ãããããšãåæãšããŠããŸãããããããããã¯æ£ç¢ºãªæå»ã®å€éšãœãŒã¹ã®äžçš®ã§ãããããã䜿çšããŠã·ã¹ãã æå»ãææ°ã®ãã®ã«ããããšãã§ããŸãããã以éã¯ãã«ãŒãã«ã«å®æçã«ïŒ11ç§ããšã«ïŒã·ã¹ãã æéã«ãŒãã«ã®æ£ããå€ãããŒããŠã§ã¢æéãããã«èœãšãããã«äŸé Œããã ãã§ãã
ããã«ã€ããŠã¯ãhwclockããŒã ããã¥ã¢ã«ãåç §ããŠãã ããã
Linuxã·ã¹ãã ãããŒããŠã§ã¢ã¯ããã¯ããã®å€ã2ã€ã®æ¹æ³ã§è§£éã§ããããšã«ãèšåãã䟡å€ãããã§ããããããã¯ãããŒããŠã§ã¢ã¯ããã¯ãæéãã«ãŠã³ã¿ãŒyyyyãMMãddãHHãmmãssã®åœ¢åŒã§ä¿åããããã§ããããŸããåè¿°ã®ããã«ãã¿ã€ã ãŸãŒã³ããªããšããããã®ã«ãŠã³ã¿ãŒãæé軞äžã®ãã€ã³ãã«çµã³ä»ããããšã¯ã§ããŸããã
å®éã«ã¯ã2ã€ã®ãªãã·ã§ã³ãããããŸãããUTCãŸãã¯ããŒã«ã«ã¿ã€ã ãŸãŒã³ïŒããã©ã«ãã§ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ã€ã³ã¹ããŒã«ãããŠããã¿ã€ã ãŸãŒã³ïŒã§ãã
éå§ããã«ã¯ãBIOSã«ç§»åããŠãã¡ã€ã³ã¡ãã¥ãŒã®æèšãèŠãŠãã ããã BIOSãšæèšã®ã¯ããã¯ãæ¯èŒããäžèŽããå ŽåãBIOSã¯ããã¯ã¯çŸå°æéã§ãïŒããã§ãšãããããŸã-Windowsã³ã³ãã¥ãŒã¿ãŒãæã£ãŠããå¯èœæ§ãé«ãã§ãïŒããããŠãããããäžèŽããªãå ŽåãBIOSã¯ããã¯ã¯UTCã¿ã€ã ãŸãŒã³ã«èšå®ãããŸãïŒãŸãã¯å¥ã®ãªãã·ã§ã³-ééã£ãŠããŸããŸãïŒã
Windowsã§ã¯ãããã©ã«ãã§ãããŒããŠã§ã¢ã¯ããã¯ãããã®æéãçŸå°æéãšäžèŽããå¿ èŠããããŸãããŸããLinuxã¯ããŒããŠã§ã¢ã¯ããã¯ã®ããŒã«ã«æéãšUTCæéã®äž¡æ¹ãç°¡åã«äœ¿çšã§ããŸãïŒåŸè ãæãŸããïŒããã®ãããååãšããŠãã·ã¹ãã ã2åèµ·åãããšãBIOSã®ã¯ããã¯ã¯Windowså°çšã®çŸå°æéã«åŸã£ãŠé²ã¿ãLinuxã¯ããã«é©å¿ããŸãã
Debian / Ubuntuã®çŸåšã®ã¢ãŒãã¯æ¬¡ã®ããã«è¡šç€ºã§ããŸãã
$ cat /etc/default/rcS | grep UTC # assume that the BIOS clock is set to UTC time (recommended) UTC=yes
ã«ãŒãæé
çŸæç¹ã§ã·ã¹ãã å šäœã®æéãšã¯ç°ãªããšæãããããã°ã©ã ãäœæããå¿ èŠãããå Žåã¯ãUbuntu / Debianãªããžããªã«ã·ã¹ãã ã³ãŒã«ãã€ã³ã¿ãŒã»ããããã³å€æŽããfaketimeãŠãŒãã£ãªãã£ãæ¢ã«ãããŸãã
$ date Wed Dec 30 22:53:11 MSK 2015 $ faketime -f '+2y' date Fri Dec 29 22:53:39 MSK 2017
èªçæ¥ããŒãã£ãŒ
圌ã4æ15æ¥ã«çãŸãããšèª°ããèšã£ãå Žåãæ¯å¹Žãã®äººã®èªçæ¥ãç¥ãæ©äŒããããŸãã 2001幎4æ15æ¥ã«çãŸãããšåœŒãèšã£ãå Žåãç§ãã¡ã¯åœŒã®å¹Žéœ¢ã«ã€ããŠããã«åŠã¶æ©äŒãåŸãŸãããã ãããããã®å Žåããããã¯è»žäžã®ã©ã®æç¹ã«ã察å¿ããŸããã第äžã«ãåºçæå»ã¯ç€ºãããŠããŸããã第äºã«ãåºçã®ã¿ã€ã ãŸãŒã³ã¯ç€ºãããŠããŸãããçè«çã«ã¯ãæ£ç¢ºãªç幎ææ¥ã瀺ãããŠããå Žåãç幎ææ¥ã®ã¿ã€ã ãŸãŒã³ãèŠã€ããããšãã§ããŸãã
ãŠãŒã¶ãŒèªèº«ã«ãç¥ãã®èšèãéãããšãã§ããæç¹ãšã圌ã®èªçæ¥ã«é¢ããéç¥ãå人ã«éä¿¡ããæç¹ãã©ã®ããã«æ£ç¢ºã«èšç®ã§ããŸããïŒãŸãã¯ã次ãææ¡ã§ããŸãã
- ãŠãŒã¶ãŒãžã®éç¥ã®éä¿¡-tïŒuïŒãåºçã®ã¿ã€ã ãŸãŒã³ã¯äžæã§ããããããŠãŒã¶ãŒã®çŸåšã®ã¿ã€ã ãŸãŒã³ã䜿çšããŸããåºçæå»ã¯äžæã§ããããããŠãŒã¶ãŒã«ãšã£ãŠå¿«é©ãªæéã䜿çšããŸãã
- å人ãžã®éç¥ã®éä¿¡-tïŒfïŒããŸãããŠãŒã¶ãŒã®ç幎ææ¥ãå人ã®ã¿ã€ã ãŸãŒã³ãå人ã®å¿«é©ãªæéãçµã¿åãããŸãã
æ¥æ¬ãã©ã³ã¹
@Test public void testBirthday() throws Exception { TimeZone japanTz = TimeZone.getTimeZone("Japan"); Calendar japanCalendar = Calendar.getInstance(japanTz); japanCalendar.setLenient(false); japanCalendar.setTimeInMillis(0); japanCalendar.set(2016, Calendar.APRIL, 15, 9, 0, 0); System.out.println("Japan 2016-04-15 09:00:00: " + japanCalendar.getTimeInMillis()); japanCalendar.set(2016, Calendar.APRIL, 15, 21, 0, 0); System.out.println("Japan 2016-04-16 21:00:00: " + japanCalendar.getTimeInMillis()); TimeZone franceTz = TimeZone.getTimeZone("Europe/France"); Calendar franceCalendar = Calendar.getInstance(franceTz); franceCalendar.setLenient(false); franceCalendar.setTimeInMillis(0); franceCalendar.set(2016, Calendar.APRIL, 14, 9, 0, 0); System.out.println("France 2016-04-14 09:00:00: " + franceCalendar.getTimeInMillis()); franceCalendar.set(2016, Calendar.APRIL, 14, 21, 0, 0); System.out.println("France 2016-04-14 21:00:00: " + franceCalendar.getTimeInMillis()); franceCalendar.set(2016, Calendar.APRIL, 15, 9, 0, 0); System.out.println("France 2016-04-15 09:00:00: " + franceCalendar.getTimeInMillis()); franceCalendar.set(2016, Calendar.APRIL, 15, 21, 0, 0); System.out.println("France 2016-04-15 21:00:00: " + franceCalendar.getTimeInMillis()); }
æ¥æ¬2016-04-15 09:00:00ïŒ1460678400000 æ¥æ¬2016-04-16 21:00:00ïŒ1460721600000 ãã©ã³ã¹2016-04-14 09:00:00ïŒ1460624400000 ãã©ã³ã¹2016-04-14 21:00:00ïŒ1460667600000 ãã©ã³ã¹2016-04-15 09:00:00ïŒ1460710800000 ãã©ã³ã¹2016-04-15 21:00:00ïŒ1460754000000
æétïŒfïŒã¯æétïŒuïŒãããã¯ããã«é·ããªãå¯èœæ§ããããŸããããšãã°ããŠãŒã¶ãŒãæ¥æ¬ã«ããŠãå人ããšãŒãããã«ããå Žåã§ãããã®å Žåãæ©æã«ãšãŒãããã®å人ã«éç¥ãå±ããŸã§ã«ãæ¥æ¬ã®ãŠãŒã¶ãŒã¯ãã§ã«DRã®ãç¥ããçµããŠããå¯èœæ§ããããŸãããã®å Žåã1æ¥åã«å人ã«éç¥ããæéãå€æŽããéç¥ãã¬ãŒãºã«ãææ¥ããšããåèªãè¿œå ã§ããŸãã
åœå¢å¹Žéœ¢
ç§ã«ãšã£ãŠéåžžã«èå³æ·±ããæ³çæ§è³ªã®ãã1ã€ã®ãã€ã³ãããããŸããå€æ°ã®å¢ç幎霢ããããŸãïŒå€§å€æ°ã®å¹Žéœ¢ãæ§çåæã®å¹Žéœ¢ãåäºè²¬ä»»ã®å¹Žéœ¢ã
誰ãããŠã©ãžãªã¹ãã¯ã§01.01.10 00:00:01ã«çãŸãããšããŸãã圌ã¯ã¢ã¹ã¯ã¯ã§2016.01.09 23:59:59ã«è»œç¯çœªãç¯ããŸããããã¹ããŒãã«èªçæ¥ïŒ2000.01.10ïŒãèšé²ãããããã³ã«ã«ç¯çœªã®æ¥ïŒ2016.01.09ïŒãèšé²ãããšã人ã¯ãŸã 16æ³ã«ãªã£ãŠããªãããšãããããŸãããã ããã¢ã¹ã¯ã¯ã§ã¯2016.01.09 23:59:59-ãŠã©ãžãªã¹ãã¯ïŒåœŒãçãŸããå ŽæïŒã§ã¯2016.01.10ã«ãªãã16æ³ã«ãªããŸããå察ã®ç¶æ³ã§ã¯ãã¢ã¹ã¯ã¯ã§çãŸããŠã©ãžãªã¹ãã¯ã§ç¯çœªãç¯ãã人ã¯ãã§ã«16æ³ã«ãªããŸãããã¢ã¹ã¯ã¯ã®æéã«å¿ããŠæ°ãããšããŸã ãªãããšãããããŸãã
åžæ³æ £è¡ã§ãã®äºä»¶ãæé€ããããã«ãäºæ³ãããç¿æ¥ã®00:00:00ããæš©å©/責任ã®æŽ»æ§åãçããèŠç¯ã䜿çšãããŸããã€ãŸããåäºè²¬ä»»ã¯2016.01.11 00:00:00ã«ã€ãã³ãã®å Žæã§çºçããŸãããã®å Žåããã®äººã¯ã©ãã«ããŠã16æ³ã§ãã
çµè«
- ã¿ã€ã ãŸãŒã³ãç¡èŠããªãã§ãã ãã
- ã¿ã€ã ãã·ã³ã®æºåãã»ãŒå®äºããŠããå ŽåãæšæºJavaã©ã€ãã©ãªã䜿çšããŠéå»ãžã®ãªãã»ãããæ£ç¢ºã«èšç®ããããšã¯ãå§ãããŸãããã¿ã€ã ã¹ã¿ã³ãã䜿çšããæ£ç¢ºãªæªæ¥ãžã®ç§»åã¯ãŸã£ããäžå¯èœã§ãã
- ãŸããJDKã®çŸåšã®ããŒãžã§ã³ã¯ãéæ²³åžåœã®å®å®è¹ã§ã®å éšèšç»ã·ã¹ãã ã®äœæã«ã¯é©ããŠããŸããã
èšäºã®2çªç®ã®éšåã¯ã Java 8ã®æ°ããDate Time APIã«ã€ããŠã§ãã