Java LocalDateTime Calculate toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone)

Here you can find the source of toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone)

Description

Converts the given local server time to the given TimeZone .

License

LGPL

Parameter

Parameter Description
localDateTime the local time that need to be converted to the given time zone
clientTimeZone the target client zone

Return

ISO8601 formatted date with included time zone offset

Declaration

public static OffsetDateTime toOffsetDateTime(Date localDateTime, TimeZone clientTimeZone) 

Method Source Code


//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();
    }
}

Related

  1. setTime(LocalDateTime source, String time)
  2. shiftDateTime(String timex, LocalDateTime reference, boolean future)
  3. shouldSend(Optional emailSentOpt, int timeToWait)
  4. sleepUntil(LocalDateTime next)
  5. toCalendar(final LocalDateTime localDateTime)
  6. WizzAirDatetimeCorrection(LocalDateTime aLocalDateTime)