最近同事W发现使用Java Date创建日期,在不同的机器上执行,得到的部分天小时数不一致。一开始怀疑机器的时间同步有问题,便拿到自己的机器上运行,异常复现,开始排查。
package com.bc.time; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; public class TimeZoneTest { public static void main(String[] args) { TimeZoneTest test = new TimeZoneTest(); final long ONE_DAY_MILLIS = 1000 * 3600 * 24L; Date date0 = new Date(ONE_DAY_MILLIS * 0); System.out.println("start date=" + test.format(date0) + "millis=" + date0.getTime()); System.out.println("********************"); int count = 0; for (int i = 0; i < 300000; ++i) { long millis = ONE_DAY_MILLIS * i; Date date = new Date(millis); if (date.getHours() != 8 || date.getMinutes() != 0 || date.getSeconds() != 0) { System.out.println("i = " + i + " | " + test.format(date) + "| " + date + ", millis=" + millis); GregorianCalendar newDate = new GregorianCalendar(); newDate.setTimeInMillis(millis); System.out.println("i = " + i + " | newDate " + newDate.getTimeInMillis() + "| " + newDate); count++; } } System.out.println("count=" + count); } public String format(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = format.format(date); return dateStr; } }
package com.bc.time; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import java.text.SimpleDateFormat; public class WhatTime { /** * Dates those have not EXACTLY 24 hours ? **/ public static void testDayTime(TimeZone timeZone){ SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Time Zone is " + timeZone.getDisplayName() + " " + timeZone.getID()); Calendar start = Calendar.getInstance(timeZone); start.setTime(new Date(0));//UTC 1970-01-01 System.out.println("start=" + fmt.format(start.getTime())); long end = Calendar.getInstance(timeZone).getTimeInMillis();//current time boolean find = false; for(long i = start.getTimeInMillis(); i < end; i= start.getTimeInMillis() ){ start.add(Calendar.DATE, 1); //add one day if((start.getTimeInMillis() - i)%(24*3600*1000L) != 0){ find = true; System.out.println("from " + fmt.format(new Date(i)) + "to " + fmt.format(start.getTime()) + " has " + (start.getTimeInMillis() - i) + "ms" + "[" + (start.getTimeInMillis() - i)/(3600*1000L) + "hours]"); } } if(!find){ System.out.println("Every day is ok."); } } public static void main(String argv[] ) throws Exception{ TimeZone timeZone = TimeZone.getDefault(); WhatTime.testDayTime(timeZone); System.out.println("----------------------------------------------------------------"); timeZone = TimeZone.getTimeZone("GMT"); WhatTime.testDayTime(timeZone); } }