本地时间和格林威治时间转换

/***
 * 转成格林威治时间
 *
 * @param LocalDate 时间格式必须是yyyy-MM-dd HH:mm:ss

 * @return
 */
public static String LocalToGTM(String LocalDate) {
    SimpleDateFormat format;
    format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date result_date;
    long result_time = 0;

    if (null == LocalDate) {
        return LocalDate;
    } else {
        try {
            format.setTimeZone(TimeZone.getDefault());
            result_date = format.parse(LocalDate);
            result_time = result_date.getTime();
            format.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
            return format.format(result_time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return LocalDate;
}

/***
 * 将当前时间转成格林威治时间戳
 *
 * @return
 */
public static String localToGTM() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    format.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
    try {
        Date currDate = Calendar.getInstance().getTime();
        XLog.e(XLog.TAG_GU, "转换前的时间:" + currDate.toString() + " : " + currDate.getTime());
        long resultTime = currDate.getTime();
        String newTime = format.format(resultTime);
        XLog.e(XLog.TAG_GU, "转换后的时间:" + newTime);
        SimpleDateFormat gmtformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        gmtformat.setTimeZone(TimeZone.getDefault());
        Date newDate = gmtformat.parse(newTime);
        XLog.e(XLog.TAG_GU, "转换后的时间2  : " + newDate.getTime());
        return "" + newDate.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return format.format(Calendar.getInstance().getTime());
}

猜你喜欢

转载自blog.csdn.net/fwt336/article/details/79247918
今日推荐