DateUtils { public static Date convertToServerDate(String userTimeZone, Date userDate) { TimeZone userLocal = TimeZone.getTimeZone(userTimeZone); int rawOffset = TimeZone.getDefault().getRawOffset() - userLocal.getRawOffset(); return new Date(userDate.getTime() + rawOffset); }  static long getCollectTimeInLong(Date date); static Date convertToServerDate(String userTimeZone, Date userDate); static Date convertToUserDate(String userTimeZone, Date serverDate); static String dateToString(Date date); static Map<String, String> getFilteredTimeZoneMap(); static Date toSimpleDate(String strDate); static Date toDate(String strDate); static Date addDay(Date date, int days); static String ms2Time(long ms); static long timeToMs(int day, int hour, int min, int sec); static boolean compareDateEndWithMinute(Date d1, Date d2);  }
@Test public void testConvertToServerDate() { String userLocaleId = "Asia/Seoul"; TimeZone tz = TimeZone.getTimeZone(userLocaleId); Date userDate = new Date(); Date serverDate = DateUtils.convertToServerDate(userLocaleId, userDate); assertThat((int) (userDate.getTime() - serverDate.getTime()), is(tz.getRawOffset() - TimeZone.getDefault().getRawOffset())); }
@Test public void testConvertToServerDateSameLocal() { String userLocaleId = "Asia/Shanghai"; TimeZone tz = TimeZone.getTimeZone(userLocaleId); Date userDate = new Date(); Date serverDate = DateUtils.convertToServerDate(userLocaleId, userDate); assertThat((int) (userDate.getTime() - serverDate.getTime()), is(tz.getRawOffset() - TimeZone.getDefault().getRawOffset())); }