Here you can find the source of toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone)
Parameter | Description |
---|---|
localDateTime | the local time that need to be converted to the given time zone |
clientTimeZone | the target client zone |
public static OffsetDateTime toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone)
//package com.java2s; //License from project: LGPL import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.Objects; import java.util.TimeZone; public class Main { /**//from w w w. j a va 2s . c o m * Converts the given local server time to the given {@link TimeZone}. The method makes sure the time is adjusted to * match the given time zone. <br> * For example, if the argument represents {@code 2007-12-03T10:30+02:00} and the given time zone offset is * {@code +03:00}, then this method will return {@code 2007-12-03T11:30+03:00}. * * @param localDateTime * the local time that need to be converted to the given time zone * @param clientTimeZone * the target client zone * @return ISO8601 formatted date with included time zone offset */ public static OffsetDateTime toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone) { Objects.requireNonNull(localDateTime, "Local time is required"); Objects.requireNonNull(clientTimeZone, "Time zone is required"); return ZonedDateTime.ofInstant(Instant.ofEpochMilli(localDateTime.getTime()), ZoneId.systemDefault()) .withZoneSameInstant(clientTimeZone.toZoneId()).toOffsetDateTime(); } }