Here you can find the source of getLocalDateTime(final long timestamp)
Parameter | Description |
---|---|
timestamp | <p> |
public static final LocalDateTime getLocalDateTime(final long timestamp)
//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); } }