Converting UTC dates to other timezones

后端 未结 5 560
礼貌的吻别
礼貌的吻别 2020-11-29 21:55

I\'m converting a UTC time to another timezone, using this method:

SimpleDateFormat format = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");
Date parsed = for         


        
5条回答
  •  忘掉有多难
    2020-11-29 22:11

    You need to take Daylight Savings into consideration. I do this by working out the offset (from UTC) in millieseconds. Something like this should work.

    int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(parsed) ? tz.getDSTSavings() : 0);
    String result = format.format(parsed.getTime() + currentOffsetFromUTC);
    

    The inDayLightTime(...) method returns a boolean and must be passed a Date object in order for it to decide if that 'date' represents one during a DST period or not.

提交回复
热议问题