Java Utililty Methods TimeZone

List of utility methods to do TimeZone

Description

The list of methods to do TimeZone are organized into topic(s).

Method

DateextractTimeZone(String strdate, Date thedate)
extract Time Zone
String tzSign = strdate.substring(strdate.length() - 6, strdate.length() - 5);
String tzHour = strdate.substring(strdate.length() - 5, strdate.length() - 3);
String tzMin = strdate.substring(strdate.length() - 2);
if (tzSign.equals("-") || tzSign.equals("+")) {
    int h = Integer.parseInt(tzHour);
    int m = Integer.parseInt(tzMin);
    if (tzSign.equals("+")) {
        h = -1 * h;
...
DategetDateAtTimeZone(Date when, TimeZone from, TimeZone to)
get Date At Time Zone
Calendar result = Calendar.getInstance();
result.setTime(when);
int offset = to.getOffset(when.getTime()) - from.getOffset(when.getTime());
result.add(Calendar.MILLISECOND, offset);
return result.getTime();
HashtablegetDateAttributes(long millisec, TimeZone tz, boolean showNumber)
get Date Attributes
Calendar cal = null;
if (tz == null)
    cal = Calendar.getInstance();
else
    cal = Calendar.getInstance(tz);
cal.setLenient(true);
cal.setTimeInMillis(millisec);
java.util.Date d = cal.getTime();
...
LonggetDateBoxValue(TimeZone zone, Date date)
Returns the value for the UTCDateBox for a specified TimeZone and Date .
if (date == null)
    return null;
Calendar cal = GregorianCalendar.getInstance(zone);
cal.setTime(date);
Calendar gmt = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
gmt.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
gmt.set(Calendar.HOUR_OF_DAY, 0);
gmt.set(Calendar.MINUTE, 0);
...
DategetDateValue(TimeZone zone, Long dateBoxValue, Long timeBoxValue)
Returns the Date for the values of the UTCDateBox and UTCTimeBox which were edited in the specified TimeZone .
if (dateBoxValue == null)
    return null;
Calendar gmt = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
gmt.setTimeInMillis(dateBoxValue.longValue());
Calendar cal = GregorianCalendar.getInstance(zone);
cal.set(gmt.get(Calendar.YEAR), gmt.get(Calendar.MONTH), gmt.get(Calendar.DAY_OF_MONTH));
int hours, minutes, extraMillis;
if (timeBoxValue != null) {
...
CalendargetDateWithoutTimeZone(Date date)
Returns a Calendar without time zone & time
if (date == null) {
    return null;
Calendar cal = getCalendarWithoutTimeZone(date);
cal.clear(Calendar.ZONE_OFFSET);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
...
doublegetExcelSerialDayNumber(Date date, Locale locale, TimeZone timeZone)
get Excel Serial Day Number
GregorianCalendar calendar = new GregorianCalendar(timeZone, locale);
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); 
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int min = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
...
java.util.DategetLocalTime(java.util.TimeZone localTimeZone, java.util.Date timestamp, java.util.TimeZone sourceTimeZone)
get Local Time
if (timestamp != null) {
    if (localTimeZone == null) {
        throw new IllegalArgumentException("localTimeZone cannot be null");
    if (sourceTimeZone == null) {
        throw new IllegalArgumentException("sourceTimeZone cannot be null");
    java.util.Calendar c = java.util.Calendar.getInstance();
...
DategetTimezoneDate(String stateCode)
get Timezone Date
final int GMT8 = 8;
Calendar calendar = Calendar.getInstance();
@SuppressWarnings("deprecation")
Date date = new Date();
date.setHours(date.getHours() + GMT8);
return date;
intgetTimeZoneOffset(Date d)
get Time Zone Offset
Calendar c = getCalendar();
c.setTime(d);
return (c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET));