Java SQL LocalDate getLocalDate()

Here you can find the source of getLocalDate()

Description

get Local Date

License

Apache License

Declaration

@Deprecated
public static final LocalDate getLocalDate() 

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;

import java.util.Date;

public class Main {
    /**//from   w  w  w  . j ava  2s  . c  o  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 };
    /**
     * The formatters we'll use to get a LocalDate.
     * <p>
     * TODO order these so the most used one is first
     */
    private static final DateTimeFormatter DATES[] = { DateTimeFormatter.ISO_DATE, DateTimeFormatter.ISO_LOCAL_DATE,
            DateTimeFormatter.ISO_OFFSET_DATE, DateTimeFormatter.BASIC_ISO_DATE };

    /**
     *
     * @return
     * @deprecated use {@link #getUTCDate()
     */
    @Deprecated
    public static final LocalDate getLocalDate() {
        return getLocalDateTime().toLocalDate();
    }

    public static final LocalDate getLocalDate(final long timestamp) {
        return getLocalDate(getInstant(timestamp));
    }

    public static final LocalDate getLocalDate(final Instant instant) {
        Clock clock = Clock.fixed(instant, UTC);
        return LocalDate.now(clock);
    }

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

    public static LocalDate getLocalDate(ResultSet rs, String n) throws SQLException {
        Date d = rs.getDate(n);
        if (rs.wasNull()) {
            return null;
        }
        return getLocalDate(d.getTime());
    }

    public static LocalDate getLocalDate(ResultSet rs, int i) throws SQLException {
        Date d = rs.getDate(i);
        if (rs.wasNull()) {
            return null;
        }
        return getLocalDate(d.getTime());
    }

    /**
     * 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. asUtilDate(Object date)
  2. asUtilDate(Object date, ZoneId zone)
  3. convertLocalDateToDatabaseDate(LocalDate localDate)
  4. getDay(Date date)
  5. getLastDate()
  6. getLocalDateFromUtilDate(Date input)
  7. getSqlDate(java.util.Date paramDate)
  8. setDate(PreparedStatement s, int col, LocalDate ld)
  9. toDate(LocalDate localDate)