Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the passed LocalDateTime object. - Java java.time

Java examples for java.time:LocalDateTime

Description

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the passed LocalDateTime object.

Demo Code


//package com.java2s;
import java.time.Instant;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.util.Date;

public class Main {
    /**/*from   ww  w. ja v a 2s.  c om*/
     * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
     * represented by the passed LocalDateTime object.
     *
     * @return  the number of milliseconds
     */
    public static long getMilliseconds(final LocalDateTime dateTime) {
        return localDateTimeToDate(dateTime).getTime();
    }

    /**
     * Converts the specified LocalDateTime object to Date.
     *
     * @param dateTime LocalDateTime object containing the date and time (JSR 310)
     * @return the created Date
     */
    public static Date localDateTimeToDate(final LocalDateTime dateTime) {
        final Instant instant = dateTime.atZone(ZoneId.systemDefault())
                .toInstant();
        return Date.from(instant);
    }
}

Related Tutorials