Java- 从时区偏移量和日期获取转换后的日期

Java- get converted date from timezone offset and date

我的客户告诉我他会向我提供时区偏差和日期。我需要相应地转换格林威治标准时间的日期。

我不确定我是否正确。我认为偏移量是指从格林威治标准时间上下多少小时,然后将差异应用到日期时间以进行转换。

请看下面我正在努力达到客户期望的内容。

TimeZone timezone = TimeZone.getTimeZone("GMT");
timezone.setRawOffset(28800000);

如果以上代码行是正确的,那么获取转换日期的代码是什么?请建议...

此致

您需要使用 Calender Class

public static Calendar convertToGmt(Calendar cal) {

    Date date = cal.getTime();
    TimeZone tz = cal.getTimeZone();

    log.debug("input calendar has date [" + date + "]");

    //Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT 
    long msFromEpochGmt = date.getTime();

    //gives you the current offset in ms from GMT at the current date
    int offsetFromUTC = tz.getOffset(msFromEpochGmt);
    log.debug("offset is " + offsetFromUTC);

    //create a new calendar in GMT timezone, set to this date and add the offset
    Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    gmtCal.setTime(date);
    gmtCal.add(Calendar.MILLISECOND, offsetFromUTC);

    log.debug("Created GMT cal with date [" + gmtCal.getTime() + "]");

    return gmtCal;
}

取自here