Java SQL LocalDate getLastDate()

Here you can find the source of getLastDate()

Description

get Last Date

License

Apache License

Declaration

public static Date getLastDate() 

Method Source Code

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

import java.sql.Date;

import java.time.LocalDate;

import java.time.Month;
import java.time.ZoneId;

import java.util.Calendar;

public class Main {
    public static final String DEFAULT_DELIMETER = "-";
    private static final ZoneId DEFAULT_ZONE_ID = ZoneId.of("Asia/Singapore");

    public static Date getLastDate() {
        LocalDate localDate = LocalDate.now();
        Date date = Date.valueOf(localDate);

        int year = getYear(date);
        Month month = getMonth(date);

        return getLastDate(month, year);
    }//from ww  w . j a v  a2  s .c o  m

    public static Date getLastDate(Month month, int year) {
        LocalDate firstDate = LocalDate.of(year, month, getLastDay(month, year));

        return toDate(firstDate);
    }

    public static Date getLastDate(int year) {
        LocalDate firstDate = LocalDate.of(year, Month.DECEMBER, 31);

        return toDate(firstDate);
    }

    public static int getYear(Date date) {
        LocalDate localDate = date.toLocalDate();

        return localDate.getYear();
    }

    public static int getYear() {
        return getYear(getDate());
    }

    public static Month getMonth(Date date) {
        LocalDate localDate = date.toLocalDate();

        return localDate.getMonth();
    }

    public static int getLastDay(Month month, int year) {
        int lastDate = month.maxLength();

        if ((month.equals(Month.FEBRUARY)) && (year % 4 != 0))
            lastDate = 28;

        return lastDate;
    }

    /**
     * Return the last day of month and year.
     * @param month {@code int}
     * @param year {@code int}
     * @return the last day
     */
    public static int getLastDay(int month, int year) {
        return getLastDay(Month.of(month), year);
    }

    public static Date toDate(LocalDate localDate) {
        return Date.valueOf(localDate);
    }

    /**
     * Buat object java.sql.Date dari hari ini.
     * 
     * @return tanggal
     */
    public static Date getDate() {
        LocalDate localDate = getNowLocalDate();

        return toDate(localDate);
    }

    /**
     * Ubah java.lang.String menjadi java.sql.Date.<br />
     * Menggunakan "-" sebagai delimeter.
     * 
     * @param dateString format = yyyy/MM/dd
     * 
     * @return tanggal
     */
    public static Date getDate(String dateString) {
        if (dateString == null || dateString.equals(""))
            return null;

        LocalDate localDate = getLocalDate(dateString);

        return toDate(localDate);
    }

    public static Date getDate(Calendar calendar) {
        int day = calendar.get(Calendar.DATE);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);

        return getDate(year, month + 1, day);
    }

    /**
     * Ubah java.lang.String menjadi java.sql.Date.
     * 
     * @param dateString format = yyyy/MM/dd
     * @param delim pemisah antar unit bulan, tanggal, dan tahun.
     * 
     * @return tanggal
     */
    public static Date getDate(String dateString, String delim) {
        if (dateString == null || dateString.equals(""))
            return null;

        LocalDate localDate = getLocalDate(dateString, delim);

        return toDate(localDate);
    }

    public static Date getDate(int year, int month, int day) {
        LocalDate localDate = getLocalDate(day, month, year);

        return toDate(localDate);
    }

    public static Date getDate(int year, Month month, int day) {
        LocalDate localDate = getLocalDate(day, month, year);

        return toDate(localDate);
    }

    /**
     * Membuat tanggal dari hari ini ditambah {@code i}.
     * 
     * @param i jumlah yang akan ditambahkan
     * 
     * @return tanggal
     */
    public static Date getDate(int i) {
        Date today = getDate();

        return add(today, i);
    }

    /**
     * Check whether two dates equals or not. Comparison between year, month, and day only.
     * @param date1
     * @param date2
     * @return true if year, month, and day are equals. Otherwise, false.
     */
    public static boolean equals(Date date1, Date date2) {
        if (getYear(date1) != getYear(date2))
            return false;
        if (getMonthInt(date1) != getMonthInt(date2))
            return false;
        return getDay(date1) == getDay(date2);
    }

    public static LocalDate getNowLocalDate() {
        return getLocalDate(DEFAULT_ZONE_ID);
    }

    public static LocalDate getLocalDate(ZoneId zoneId) {
        return LocalDate.now(zoneId);
    }

    /**
     * Format yyyy/MM/dd
     * @param dateString
     * @return
     */
    public static LocalDate getLocalDate(String dateString) {
        return getLocalDate(dateString, DEFAULT_DELIMETER);
    }

    /**
     * Format yyyy/MM/dd
     * @param dateString
     * @param delim
     * @return
     */
    public static LocalDate getLocalDate(String dateString, String delim) {
        if (dateString == null || dateString.equals(""))
            return null;

        // year-month-date
        String elStr[] = dateString.split(delim);

        return getLocalDate(Integer.parseInt(elStr[2]), getMonthInt(elStr[1]), Integer.parseInt(elStr[0]));
    }

    public static LocalDate getLocalDate(int day, int month, int year) {
        int lastDay = getLastDay(month, year);
        int antara = day - lastDay;

        if (antara > 0) {
            month++;
            day = antara;
        }

        return LocalDate.of(year, month, day);
    }

    public static LocalDate getLocalDate(int day, Month month, int year) {
        int lastDay = getLastDay(month, year);
        int antara = day - lastDay;

        if (antara > 0) {
            int monthValue = month.getValue();
            month = Month.of(++monthValue);
            day = antara;
        }

        return LocalDate.of(year, month, day);
    }

    public static Date add(Date awal, int i) {
        LocalDate localDate = awal.toLocalDate();

        LocalDate newDate = localDate.plusDays(i);

        return toDate(newDate);
    }

    /**
     * Return month in {@code int} representation from {@code String}.
     * @param month
     * @return month in {@code int} representation
     */
    public static int getMonthInt(String month) {
        return Integer.parseInt(month);
    }

    public static int getMonthInt(Date date) {
        Month month = getMonth(date);

        return month.getValue();
    }

    public static int getDay(Date date) {
        LocalDate localDate = date.toLocalDate();

        return localDate.getDayOfMonth();
    }
}

Related

  1. asLocalDate(java.util.Date date, ZoneId zone)
  2. asUtilDate(Object date)
  3. asUtilDate(Object date, ZoneId zone)
  4. convertLocalDateToDatabaseDate(LocalDate localDate)
  5. getDay(Date date)
  6. getLocalDate()
  7. getLocalDateFromUtilDate(Date input)
  8. getSqlDate(java.util.Date paramDate)
  9. setDate(PreparedStatement s, int col, LocalDate ld)