All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mavenreposs.php.functions.strtotime.GMTDate Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
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