List of utility methods to do TimeZone
int | getTimezoneOffsetInHours(Date date) Gets the timezone offset in hours. int x = getTimezoneOffsetInMinutes(date); int inHours = x / 60; return inHours; |
boolean | hasTimeComponent(Date date, TimeZone timeZone) has Time Component final Calendar cal = Calendar.getInstance(); cal.setTimeZone(timeZone); cal.setTime(date); cal.getTime(); return (cal.get(Calendar.HOUR_OF_DAY) != 0) || (cal.get(Calendar.MINUTE) != 0); |
Date | midnight(Date date, TimeZone tz) Calculates midnight of the day in which date lies with respect to a time zone. Calendar cal = new GregorianCalendar(tz); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); |
Calendar | monthOfYear(Date referenceDate, int month, TimeZone timeZone) Utility function to get the start of the month as a Calendar. Calendar c1 = Calendar.getInstance(timeZone);
c1.setTime(referenceDate);
Calendar c2 = Calendar.getInstance(timeZone);
c2.clear();
c2.set(c1.get(Calendar.YEAR), month - 1, 1);
return c2;
|
Date | normalizeDate(Date date, TimeZone timeZone) normalize Date Calendar calendar = new GregorianCalendar(timeZone); calendar.setTime(date); calendar.set(Calendar.AM_PM, Calendar.AM); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); ... |
Date | startOfNextDay(final Date date, final TimeZone timeZone) start Of Next Day final Calendar cal = Calendar.getInstance(timeZone); cal.setTime(date); cal.add(Calendar.DATE, 1); cal.set(Calendar.HOUR, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 1); return cal.getTime(); |
Calendar | startOfYear(Date referenceDate, TimeZone timeZone) Calculate the date at the start of the year for the given date. Calendar c1 = Calendar.getInstance(timeZone);
c1.setTime(referenceDate);
Calendar c2 = Calendar.getInstance(timeZone);
c2.clear();
c2.set(c1.get(Calendar.YEAR), 0, 1);
return c2;
|
Long | toEpoch(Date dateTime, String timeZone) to Epoch Calendar timeOffset = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); timeOffset.set(Calendar.MILLISECOND, 0); timeOffset.set(Calendar.SECOND, 0); timeOffset.set(Calendar.MINUTE, 0); timeOffset.set(Calendar.HOUR_OF_DAY, 0); long midnightOffSet = timeOffset.getTime().getTime(); long localTimestamp = dateTime.getTime(); return timeOffset == null ? null : midnightOffSet + localTimestamp; ... |