io.github.mavenreposs.php.functions.strtotime.GMTDate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of php-functions Show documentation
Show all versions of php-functions Show documentation
php functions for java implement.
package io.github.mavenreposs.php.functions.strtotime;
import java.util.Date;
import java.util.TimeZone;
public class GMTDate {
/**
* 转换时间到GMT时区
* @param date Date
* @return Date
*/
public static Date convertToGmt(Date date) {
TimeZone tz = TimeZone.getDefault();
Date ret = new Date(date.getTime() - tz.getRawOffset());
// if we are now in DST, back off by the delta. Note that we are checking the GMT date, this is the KEY.
if (tz.inDaylightTime(ret)) {
Date dstDate = new Date(ret.getTime() - tz.getDSTSavings());
// check to make sure we have not crossed back into standard time
// this happens when we are on the cusp of DST (7pm the day before the change for PDT)
if (tz.inDaylightTime(dstDate)) {
ret = dstDate;
}
}
return ret;
}
/**
* 转换时间来自GMT时区
* @param date Date
* @return Date
*/
public static Date convertFromGmt(Date date) {
TimeZone tz = TimeZone.getDefault();
Date ret = new Date(date.getTime() + tz.getRawOffset());
// if we are now in DST, back off by the delta. Note that we are checking the GMT date, this is the KEY.
if (tz.inDaylightTime(ret)) {
Date dstDate = new Date(ret.getTime() + tz.getDSTSavings());
// check to make sure we have not crossed back into standard time
// this happens when we are on the cusp of DST (7pm the day before the change for PDT)
if (tz.inDaylightTime(dstDate)) {
ret = dstDate;
}
}
return ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy