我是一个Java爱好者
分类: Java
2013-11-06 18:04:26
/**
* Get relative millisecond offset of two dates in different time zones.
* Note that the offset value is calculated as the two dates compared in a
* same time zone.
*
* @param date1
* The first date.
* @param timeZone1
* The first time zone which the first date is in.
* @param date2
* The second date.
* @param timeZone2
* The second time zone which the second date is in.
* @return Millisecond offset value between the two dates. the value 0 if
* date2 is equal to date1; a value greater than 0 if date2 is after
* date1; and a value less than 0 if date2 is before date1.
*/
public static long getDatesOffsetWithTimeZone(Date date1, TimeZone timeZone1, Date date2,
TimeZone timeZone2)
{
long timeZoneOffset = timeZone2.getOffset(date2.getTime())
- timeZone1.getOffset(date1.getTime());
// The time millisecond offset between the two dates.
long millisOffset = date2.getTime() - date1.getTime();
// The relative time millisecond offset in a same time zone.
long timeOffset = millisOffset - timeZoneOffset;
return timeOffset;
}