package com.glede.rads.common.date;
import java.util.TimeZone;
/**
* @author hanyuan
* @createdDate 2014-8-5
*/
public enum TimeZoneID {
/**
* Greenwich Mean Time (GMT) or Universal Time Coordinated (UTC)
*/
GMT("GMT", "GMT", "GMT", "GMT"),
/**
* (America) Eastern Time
*/
EASTERN("EST", "GMT-05:00", "EDT", "GMT-04:00"),
/**
* (America) Central Time
*/
CENTRAL("CST", "GMT-06:00", "CDT", "GMT-05:00"),
/**
* (America) Mountain Time
*/
MOUNTAIN("MST", "GMT-07:00", "MDT", "GMT-06:00"),
/**
* (America) Pacific Time
*/
PACIFIC("PST", "GMT-08:00", "PDT", "GMT-07:00");
private String stAbbrID; // abbreviation id of standard time
private String stCustomID; // custom id of standard time, should like: GMT, GMT-08:00 or GMT+08:00
private String dstAbbrID; // abbreviation id of daylight saving time
private String dstCustomID; // custom id of daylight saving time, should like: GMT, GMT-08:00 or GMT+08:00
/**
* Construct TimeZoneID object
*
* @param stAbbrID abbreviation id of standard time
* @param stCustomID custom id of standard time, should like: GMT, GMT-08:00 or GMT+08:00
* @param dstAbbrID abbreviation id of daylight saving time
* @param dstCustomID custom id of daylight saving time, should like: GMT, GMT-08:00 or GMT+08:00
*/
private TimeZoneID(String stAbbrID, String stCustomID, String dstAbbrID, String dstCustomID) {
this.stAbbrID = stAbbrID;
this.stCustomID = stCustomID;
this.dstAbbrID = dstAbbrID;
this.dstCustomID = dstCustomID;
}
/**
* Get abbreviation id of standard time
*/
public String getSTAbbrID() {
return stAbbrID;
}
/**
* Get abbreviation id of daylight saving time
*/
public String getDSTAbbrID() {
return dstAbbrID;
}
/**
* Get timezone
*
* @param useDST use daylight saving time or not
*/
public TimeZone getTimeZone(boolean useDST) {
return useDST ? TimeZone.getTimeZone(dstCustomID) : TimeZone.getTimeZone(stCustomID);
}
}
package com.glede.rads.common.date;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* @author hanyuan
* @createdDate 2014-8-5
*/
public class TimeZoneUtils {
/**
* Convert a date from source time zone to target time zone
*
* @param date Date
* @param fromTimeZone source time zone
* @param toTimeZone target time zone
*/
public static Date convert(Date date, TimeZone fromTimeZone, TimeZone toTimeZone) {
Calendar fromCalendar = Calendar.getInstance(fromTimeZone);
fromCalendar.setTime(date);
Calendar toCalendar = Calendar.getInstance(toTimeZone);
toCalendar.setTime(date);
int offset = toTimeZone.getRawOffset() + toCalendar.get(Calendar.DST_OFFSET) - fromTimeZone.getRawOffset() - fromCalendar.get(Calendar.DST_OFFSET);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MILLISECOND, offset);
return calendar.getTime();
}
/**
* Convert a date from current host time zone to target time zone
*
* @param date Date
* @param toTimeZone target time zone
*/
public static Date convert(Date date, TimeZone toTimeZone) {
TimeZone fromTimeZone = TimeZone.getDefault();
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from source time zone to target time zone
*
* @param date Date
* @param fromTimeZoneID source time zone
* @param fromUseDST whether the source time zone use daylight saving time or not
* @param toTimeZoneID target time zone
* @param toUseDST whether the target time zone use daylight saving time or not
*/
public static Date convert(Date date, TimeZoneID fromTimeZoneID, boolean fromUseDST, TimeZoneID toTimeZoneID, boolean toUseDST) {
TimeZone fromTimeZone = fromTimeZoneID.getTimeZone(fromUseDST);
TimeZone toTimeZone = toTimeZoneID.getTimeZone(toUseDST);
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from current host time zone to target time zone
*
* @param date Date
* @param toTimeZoneID target time zone
* @param toUseDST whether the target time zone use daylight saving time or not
*/
public static Date convert(Date date, TimeZoneID toTimeZoneID, boolean toUseDST) {
TimeZone fromTimeZone = TimeZone.getDefault();
TimeZone toTimeZone = toTimeZoneID.getTimeZone(toUseDST);
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from UTC time zone to target time zone
*
* @param date UTC date, the time zone of date is TimeZoneID.GMT
* @param toTimeZoneID target time zone
* @param toUseDST whether the target time zone use daylight saving time or not
*/
public static Date convertFromUTC(Date date, TimeZoneID toTimeZoneID, boolean toUseDST) {
TimeZone fromTimeZone = TimeZoneID.GMT.getTimeZone(false);
TimeZone toTimeZone = toTimeZoneID.getTimeZone(toUseDST);
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from UTC time zone to current host time zone
*
* @param date UTC date, the time zone of date is TimeZoneID.GMT
*/
public static Date convertFromUTC(Date date) {
TimeZone fromTimeZone = TimeZoneID.GMT.getTimeZone(false);
TimeZone toTimeZone = TimeZone.getDefault();
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from current host time zone to UTC time zone
*
* @param date Date
*/
public static Date convertToUTC(Date date) {
TimeZone fromTimeZone = TimeZone.getDefault();
TimeZone toTimeZone = TimeZoneID.GMT.getTimeZone(false);
return convert(date, fromTimeZone, toTimeZone);
}
}
import java.util.TimeZone;
/**
* @author hanyuan
* @createdDate 2014-8-5
*/
public enum TimeZoneID {
/**
* Greenwich Mean Time (GMT) or Universal Time Coordinated (UTC)
*/
GMT("GMT", "GMT", "GMT", "GMT"),
/**
* (America) Eastern Time
*/
EASTERN("EST", "GMT-05:00", "EDT", "GMT-04:00"),
/**
* (America) Central Time
*/
CENTRAL("CST", "GMT-06:00", "CDT", "GMT-05:00"),
/**
* (America) Mountain Time
*/
MOUNTAIN("MST", "GMT-07:00", "MDT", "GMT-06:00"),
/**
* (America) Pacific Time
*/
PACIFIC("PST", "GMT-08:00", "PDT", "GMT-07:00");
private String stAbbrID; // abbreviation id of standard time
private String stCustomID; // custom id of standard time, should like: GMT, GMT-08:00 or GMT+08:00
private String dstAbbrID; // abbreviation id of daylight saving time
private String dstCustomID; // custom id of daylight saving time, should like: GMT, GMT-08:00 or GMT+08:00
/**
* Construct TimeZoneID object
*
* @param stAbbrID abbreviation id of standard time
* @param stCustomID custom id of standard time, should like: GMT, GMT-08:00 or GMT+08:00
* @param dstAbbrID abbreviation id of daylight saving time
* @param dstCustomID custom id of daylight saving time, should like: GMT, GMT-08:00 or GMT+08:00
*/
private TimeZoneID(String stAbbrID, String stCustomID, String dstAbbrID, String dstCustomID) {
this.stAbbrID = stAbbrID;
this.stCustomID = stCustomID;
this.dstAbbrID = dstAbbrID;
this.dstCustomID = dstCustomID;
}
/**
* Get abbreviation id of standard time
*/
public String getSTAbbrID() {
return stAbbrID;
}
/**
* Get abbreviation id of daylight saving time
*/
public String getDSTAbbrID() {
return dstAbbrID;
}
/**
* Get timezone
*
* @param useDST use daylight saving time or not
*/
public TimeZone getTimeZone(boolean useDST) {
return useDST ? TimeZone.getTimeZone(dstCustomID) : TimeZone.getTimeZone(stCustomID);
}
}
package com.glede.rads.common.date;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* @author hanyuan
* @createdDate 2014-8-5
*/
public class TimeZoneUtils {
/**
* Convert a date from source time zone to target time zone
*
* @param date Date
* @param fromTimeZone source time zone
* @param toTimeZone target time zone
*/
public static Date convert(Date date, TimeZone fromTimeZone, TimeZone toTimeZone) {
Calendar fromCalendar = Calendar.getInstance(fromTimeZone);
fromCalendar.setTime(date);
Calendar toCalendar = Calendar.getInstance(toTimeZone);
toCalendar.setTime(date);
int offset = toTimeZone.getRawOffset() + toCalendar.get(Calendar.DST_OFFSET) - fromTimeZone.getRawOffset() - fromCalendar.get(Calendar.DST_OFFSET);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MILLISECOND, offset);
return calendar.getTime();
}
/**
* Convert a date from current host time zone to target time zone
*
* @param date Date
* @param toTimeZone target time zone
*/
public static Date convert(Date date, TimeZone toTimeZone) {
TimeZone fromTimeZone = TimeZone.getDefault();
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from source time zone to target time zone
*
* @param date Date
* @param fromTimeZoneID source time zone
* @param fromUseDST whether the source time zone use daylight saving time or not
* @param toTimeZoneID target time zone
* @param toUseDST whether the target time zone use daylight saving time or not
*/
public static Date convert(Date date, TimeZoneID fromTimeZoneID, boolean fromUseDST, TimeZoneID toTimeZoneID, boolean toUseDST) {
TimeZone fromTimeZone = fromTimeZoneID.getTimeZone(fromUseDST);
TimeZone toTimeZone = toTimeZoneID.getTimeZone(toUseDST);
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from current host time zone to target time zone
*
* @param date Date
* @param toTimeZoneID target time zone
* @param toUseDST whether the target time zone use daylight saving time or not
*/
public static Date convert(Date date, TimeZoneID toTimeZoneID, boolean toUseDST) {
TimeZone fromTimeZone = TimeZone.getDefault();
TimeZone toTimeZone = toTimeZoneID.getTimeZone(toUseDST);
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from UTC time zone to target time zone
*
* @param date UTC date, the time zone of date is TimeZoneID.GMT
* @param toTimeZoneID target time zone
* @param toUseDST whether the target time zone use daylight saving time or not
*/
public static Date convertFromUTC(Date date, TimeZoneID toTimeZoneID, boolean toUseDST) {
TimeZone fromTimeZone = TimeZoneID.GMT.getTimeZone(false);
TimeZone toTimeZone = toTimeZoneID.getTimeZone(toUseDST);
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from UTC time zone to current host time zone
*
* @param date UTC date, the time zone of date is TimeZoneID.GMT
*/
public static Date convertFromUTC(Date date) {
TimeZone fromTimeZone = TimeZoneID.GMT.getTimeZone(false);
TimeZone toTimeZone = TimeZone.getDefault();
return convert(date, fromTimeZone, toTimeZone);
}
/**
* Convert a date from current host time zone to UTC time zone
*
* @param date Date
*/
public static Date convertToUTC(Date date) {
TimeZone fromTimeZone = TimeZone.getDefault();
TimeZone toTimeZone = TimeZoneID.GMT.getTimeZone(false);
return convert(date, fromTimeZone, toTimeZone);
}
}