seconds Since Unix Epoch - Java java.time

Java examples for java.time:LocalDateTime

Description

seconds Since Unix Epoch

Demo Code


//package com.java2s;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class Main {
    /**//ww  w .  j a v a  2s .co m
     * @param date
     *            Date for which seconds since the epoch date is to be calculated
     * @return Number of seconds after the unix epoch date equivalent to the given date
     */
    public static Integer secondsSinceUnixEpoch(final LocalDateTime date) {
        if (date == null) {
            return null;
        }
        final Long timeInSeconds = Long.valueOf(date
                .toEpochSecond(ZoneOffset.UTC));
        return Integer.valueOf(timeInSeconds.intValue());
    }
}

Related Tutorials