Java examples for java.time:Month
Convert the full element date into simple element only containing year, month, and day.
//package com.java2s; import java.time.LocalDate; import java.time.Month; import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; public class Main { public static final int EPOCH_YEAR = 1970; public static final int EPOCH_MONTH = 1; public static final int EPOCH_DAY = 1; public static final long DAY_IN_MILIS = 86400000L; /**// w ww.ja va 2 s . co m * Convert the full element date into simple element only containing year, month, and day. * @param date * @return simpleDate simpler version of date */ public static Date getSimpleDate(Date date) { int[] arrOfDate = createArrayOfDate(date); return getDate(arrOfDate[2], arrOfDate[1], arrOfDate[0]); } /** * Extract date element (year, month, and date) into array of {@code int}. * @param date * @return arrOfDate array of date [DD, mm, YYYY] */ public static int[] createArrayOfDate(Date date) { int[] arrOfDate = new int[3]; arrOfDate[0] = getDay(date); arrOfDate[1] = getMonthInt(date); arrOfDate[2] = getYear(date); return arrOfDate; } /** * Create date object. * @param year {@code int} * @param month {@code int} * @param day {@code int} * @return date object */ public static Date getDate(int year, int month, int day) { Calendar cal = getCalendar(); cal.setTimeInMillis(toMilis(year, month, day)); return cal.getTime(); } /** * Create date from String value.<br /> * Format of String is MM/dd/YYYY. * @param dateStr String value with format MM/dd/YYYY * @return date */ public static Date getDate(String dateStr) { String delim = dateStr.contains("-") ? "-" : "/"; return getDate(dateStr, delim); } /** * Create date from String value.<br /> * Format of String is MM/dd/YYYY. * @param dateStr String value with format MM/dd/YYYY * @param delim * @return date */ public static Date getDate(String dateStr, String delim) { String elStr[] = dateStr.split(delim); Date date = getDate(Integer.parseInt(elStr[2]), getMonthInt(elStr[0]), Integer.parseInt(elStr[1])); return date; } /** * Create date object. * @param year {@code int} * @param month {@code Month} * @param date {@code int} * @return */ public static Date getDate(int year, Month month, int date) { return getDate(year, month.getValue(), date); } /** * Return day in {@code int} representation from {@code Calendar}. * @param calendar * @return day in {@code int} representation */ public static int getDay(Calendar calendar) { return calendar.get(Calendar.DATE); } /** * Return day in {@code int} representation from {@code Date}. * @param day * @return day in {@code int} representation */ public static int getDay(Date date) { return getCalendar(date).get(Calendar.DATE); } /** * Return month in {@code int} representation from {@code Calendar}.<br /> * @param calendar * @return month in {@code int} representation */ public static int getMonthInt(Calendar calendar) { return calendar.get(Calendar.MONTH) + 1; } /** * 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); } /** * Return month in {@code int} representation from {@code Date}. * @param date * @return month in {@code int} representation */ public static int getMonthInt(Date date) { return getMonthInt(getCalendar(date)); } /** * Return year in {@code int} representation from {@code Calendar}. * @param calendar * @return year in {@code int} representation */ public static int getYear(Calendar calendar) { return calendar.get(Calendar.YEAR); } /** * Return year in {@code int} representation from {@code Date}. * @param date * @return year in {@code int} representation */ public static int getYear(Date date) { return getCalendar(date).get(Calendar.YEAR); } /** * Create default calendar.<br /> * @return default calendar */ public static Calendar getCalendar() { return Calendar.getInstance(); } /** * Create calendar using date.<br /> * @param date * @return calendar */ public static Calendar getCalendar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } /** * Return date representation in millisecond. * @param date * @return date representation in millisecond. */ public static long toMilis(Date date) { int year = getYear(date); int month = getMonthInt(date); int day = getDay(date); return toMilis(year, month, day); } /** * Return date representation in millisecond. * @param year * @param month * @param day * @return date representation in millisecond */ public static long toMilis(int year, int month, int day) { LocalDate epoch = LocalDate.of(EPOCH_YEAR, EPOCH_MONTH, EPOCH_DAY); LocalDate created = LocalDate.of(year, month, day); long p = ChronoUnit.DAYS.between(epoch, created); return p * DAY_IN_MILIS; } /** * Check whether {@code date1} is beetween {@code date2} and {@code date3}. * @param date1 main date. * @param date2 camparer. * @param date3 comporer. * @return */ public static boolean between(Date date1, Date date2, Date date3) { long date1Milis = toMilis(date1); long date2Milis = toMilis(date2); long date3Milis = toMilis(date3); if (date1Milis >= date2Milis && date1Milis <= date3Milis) return true; return false; } }