Java SQL Date Get getDate()

Here you can find the source of getDate()

Description

Buat object java.sql.Date dari hari ini.

License

Apache License

Return

tanggal

Declaration

public static Date getDate() 

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");

    /**// ww w  . j  av  a2 s  . c  om
     * 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);
    }

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

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

    /**
     * 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 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);
    }

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

        return localDate.getYear();
    }

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

    /**
     * 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();
    }

    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 Month getMonth(Date date) {
        LocalDate localDate = date.toLocalDate();

        return localDate.getMonth();
    }
}

Related

  1. getDate()
  2. getDate()
  3. getDate()
  4. getDate()
  5. getDate()
  6. getDate()
  7. getDate(Calendar pCal)