Java Timestamp Field getLocalDateTime(final long timestamp)

Here you can find the source of getLocalDateTime(final long timestamp)

Description

Gets the LocalDateTime for a timestamp in UTC

License

Apache License

Parameter

Parameter Description
timestamp <p>

Declaration

public static final LocalDateTime getLocalDateTime(final long timestamp) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Timestamp;

import java.time.Clock;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Main {
    /**//from  www.j  a v  a 2 s. co  m
     * {@link ZoneId} for UTC which everything in the system runs under
     */
    public static final ZoneId UTC = ZoneId.of("UTC");
    /**
     * The formatters we'll use to get a LocalDateTime.
     * <p>
     * TODO order these so the most used one is first
     */
    private static final DateTimeFormatter DATETIMES[] = { DateTimeFormatter.ISO_DATE_TIME,
            DateTimeFormatter.ISO_LOCAL_DATE_TIME, DateTimeFormatter.ISO_INSTANT,
            DateTimeFormatter.ISO_OFFSET_DATE_TIME, DateTimeFormatter.ISO_ZONED_DATE_TIME,
            DateTimeFormatter.RFC_1123_DATE_TIME };

    /**
     * Gets the {@link LocalDateTime} for a timestamp in UTC
     * <p>
     * @param timestamp <p>
     * @return
     */
    public static final LocalDateTime getLocalDateTime(final long timestamp) {
        return getLocalDateTime(getInstant(timestamp));
    }

    /**
     * Gets the current time in UTC
     * <p>
     * @return
     * @deprecated use {@link #getUTCDateTime()
     */
    @Deprecated
    public static final LocalDateTime getLocalDateTime() {
        return getLocalDateTime(Instant.now());
    }

    /**
     * Get's the {@link LocalDateTime} for an {@link Instant} in UTC
     * <p>
     * @param instant <p>
     * @return
     */
    public static final LocalDateTime getLocalDateTime(final Instant instant) {
        Clock clock = Clock.fixed(instant, UTC);
        return LocalDateTime.now(clock);
    }

    public static LocalDateTime getLocalDateTime(final LocalDate date) {
        return LocalDateTime.of(date, LocalTime.MIN);
    }

    /**
     * Attempt to parse a string
     * <p>
     * @param s <p>
     * @return
     */
    public static final LocalDateTime getLocalDateTime(final String s) {
        if (s != null && !s.isEmpty()) {
            for (DateTimeFormatter dtf : DATETIMES) {
                try {
                    return LocalDateTime.parse(s, dtf);
                } catch (DateTimeParseException ex) {
                    // Ignore
                }
            }
        }
        return null;
    }

    public static LocalDateTime getLocalDateTime(ResultSet rs, String col) throws SQLException {
        Timestamp t = rs.getTimestamp(col);
        if (t == null) {
            return null;
        }
        return t.toLocalDateTime();
    }

    public static Instant getInstant(long timestamp) {
        return Instant.ofEpochMilli(timestamp);
    }
}

Related

  1. getHoraHHMM(Timestamp dataHoraRef)
  2. getInterval(ArrayList times)
  3. getIntervalInDays(Timestamp from, Timestamp thru)
  4. getISOTimeStampStringFormat(Timestamp ts)
  5. getKDCTStoTimestamp(short[] in)
  6. getLocalTime(final Timestamp timestamp)
  7. getMessageTimestamp(int id)
  8. getMillTimestamp()
  9. getMonthEnd(Timestamp stamp, TimeZone timeZone, Locale locale)