DateUtils { public static String dateToStr(Date date) { return dateToStr(date, DATE_FORMAT_TIME); }  static int getDelayToNextMinute(long rightnow); static long getPreMinuteMills(long rightnow); static String dateToStr(Date date); static String dateToStr(Date date, String format); static Date strToDate(String dateStr); static Long strToLong(String dateStr); static Date strToDate(String dateStr, String format); static String dateToMillisStr(Date date); static Date millisStrToDate(String millisDateStr); static final int MILLISECONDS_PER_SECONDE; static final int MILLISECONDS_PER_MINUTE; static final int MILLISECONDS_PER_HOUR; static final long MILLISECONDS_PER_DAY; static final String DATE_FORMAT_TIME; static final String DATE_FORMAT_MILLS_TIME; }
@Test public void dateToStr() throws Exception { long s1 = 1501127802975l; long s2 = 1501127835658l; TimeZone timeZone = TimeZone.getDefault(); Date date0 = new Date(0 - timeZone.getOffset(0)); Date date1 = new Date(s1 - timeZone.getOffset(s1)); Date date2 = new Date(s2 - timeZone.getOffset(s2)); Assert.assertEquals(DateUtils.dateToStr(date0), "1970-01-01 00:00:00"); Assert.assertEquals(DateUtils.dateToStr(date1), "2017-07-27 03:56:42"); Assert.assertEquals(DateUtils.dateToStr(date2), "2017-07-27 03:57:15"); }
@Test public void dateToStr1() throws Exception { long d0 = 0l; long d1 = 1501127802975l; long d2 = 1501127835658l; TimeZone timeZone = TimeZone.getDefault(); Date date0 = new Date(d0 - timeZone.getOffset(d0)); Date date1 = new Date(d1 - timeZone.getOffset(d1)); Date date2 = new Date(d2 - timeZone.getOffset(d2)); Assert.assertEquals(DateUtils.dateToStr(date0, DateUtils.DATE_FORMAT_MILLS_TIME), "1970-01-01 00:00:00.000"); Assert.assertEquals(DateUtils.dateToStr(date1, DateUtils.DATE_FORMAT_MILLS_TIME), "2017-07-27 03:56:42.975"); Assert.assertEquals(DateUtils.dateToStr(date2, DateUtils.DATE_FORMAT_MILLS_TIME), "2017-07-27 03:57:15.658"); }