Java ZonedDateTime Calculate convertForPersistence(@Nullable ZonedDateTime dt)

Here you can find the source of convertForPersistence(@Nullable ZonedDateTime dt)

Description

Convert the given ZonedDateTime to a UTC date for persistence

License

Open Source License

Parameter

Parameter Description
dt the ZonedDateTime to convert to UTC

Return

a Date object that represents the same instant as the ZonedDateTime, but at UTC.

Declaration

@Nullable
public static Date convertForPersistence(@Nullable ZonedDateTime dt) 

Method Source Code

//package com.java2s;
/*//from   w ww  . j a  v a2  s  .  c o  m
 * Copyright (c) Interactive Information R & D (I2RD) LLC.
 * All Rights Reserved.
 *
 * This software is confidential and proprietary information of
 * I2RD LLC ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with I2RD.
 */

import javax.annotation.Nullable;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /** UTC. */
    public static final TimeZone UTC = TimeZone.getTimeZone(ZoneOffset.UTC);

    /**
     * Convert the given ZonedDateTime to a UTC date for persistence
     *
     * @param dt the ZonedDateTime to convert to UTC
     *
     * @return a Date object that represents the same instant as the ZonedDateTime, but at UTC.
     */
    @Nullable
    public static Date convertForPersistence(@Nullable ZonedDateTime dt) {
        if (dt == null)
            return null;
        ZonedDateTime atUtc = dt.withZoneSameInstant(ZoneOffset.UTC);
        return new Date(atUtc.toInstant().toEpochMilli());
    }
}

Related

  1. calculateTimeForSunrise(ZonedDateTime from, ZonedDateTime to)
  2. canonicalTimeString(ZonedDateTime time)
  3. cloneZonedDateTime(ZonedDateTime zonedDateTime)
  4. computeDays(ZonedDateTime Start, ZonedDateTime End)
  5. convertDateToZonedDateTime(Date date, String timezone)
  6. currentZonedDateTime()
  7. difference(ZonedDateTime t1, ZonedDateTime t2)
  8. diffMs(ZonedDateTime date1, ZonedDateTime date2)
  9. elapsedTime(ZonedDateTime zonedDateTime)