Java examples for java.time:Month
Return the last day of month and year.
import java.sql.Time; import java.sql.Date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; import java.time.temporal.ChronoUnit; public class Main{ public static int getLastDay(Month month, int year) { int lastDate = month.maxLength(); if ((month.equals(Month.FEBRUARY)) && (year % 4 != 0)) lastDate = 28;/*from ww w . ja va 2s.c om*/ 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); } /** * 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; if (getDay(date1) != getDay(date2)) return false; return true; } 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(); } /** * 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); } /** * 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 Month getMonth(Date date) { LocalDate localDate = date.toLocalDate(); return localDate.getMonth(); } }