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

时间:2015-02-05 19:27:49

标签: java datetime timezone timezone-offset

我的客户告诉我他会给我时区偏移和日期。我需要相应地在GMT中转换日期。

我不确定我是否正确。我认为胶印意味着从格林尼治标准时间上下起几个小时,并且在获得转换后的日期时间差异。

请在下面查看我想要达到客户期望的内容。

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

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

此致

1 个答案:

答案 0 :(得分:0)

您需要使用Calender

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

相关问题