Java LocalDateTime to localDateTimeToEpochMillis(TemporalAccessor temporal)

Here you can find the source of localDateTimeToEpochMillis(TemporalAccessor temporal)

Description

Convert a TemporalAccess to an Epoch value.

License

Open Source License

Parameter

Parameter Description
temporal a parameter

Return

The Epoch time

Declaration

public static long localDateTimeToEpochMillis(TemporalAccessor temporal) 

Method Source Code


//package com.java2s;
/* //from www.  j a  v  a 2s.  c om
 * Copyright (C) 2016 Bruce Beisel
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAccessor;

public class Main {
    /**
     * Convert a TemporalAccess to an Epoch value.
     * 
     * @param temporal
     * @return The Epoch time
     */
    public static long localDateTimeToEpochMillis(TemporalAccessor temporal) {
        return temporalToInstant(temporal).toEpochMilli();
    }

    /**
     * Convert a temporal accessor to an Instant.
     * 
     * @param temporal The temporal to convert
     * 
     * @return The Instant
     */
    public static Instant temporalToInstant(TemporalAccessor temporal) {
        //
        // LocalDateTime, LocalDate and LocalTime must have a time zone conversion before being converted to an Instant
        //
        Instant instant;
        if (temporal instanceof LocalDateTime)
            instant = ((LocalDateTime) temporal).atZone(ZoneId.systemDefault()).toInstant();
        else if (temporal instanceof LocalDate)
            instant = ((LocalDate) temporal).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        else if (temporal instanceof LocalTime)
            instant = ((LocalTime) temporal).atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant();
        else
            instant = Instant.from(temporal); // throws DateTimeException

        return instant;
    }
}

Related

  1. getHoursElapsed(LocalDateTime fromDate)
  2. getMillis(LocalDateTime time)
  3. getMinuteOfDay(LocalDateTime ldt)
  4. getSecondsSinceJavaEpoch(LocalDateTime localDateTime)
  5. localDateTimeToDate(LocalDateTime ldt)
  6. localDateTimeToString(LocalDateTime value)
  7. localDateTimeToTimestamp(LocalDateTime ldt)
  8. longTOLocalDateTime(long minDate)
  9. luisDate(LocalDateTime date)