Java examples for java.time:Today
Return today in String representation.
import java.time.LocalDate; import java.time.Month; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class Main{ /**/*from www . j av a2 s . c o m*/ * Return today in String representation. * @return today in String representation using format MM/dd/YYYY */ public static String getSimpleNowInString() { return toString(getSimpleNow()); } /** * Return today in String representation with specified delim. * @return today in String representation using format MM [delim] dd [delim] YYYY */ public static String getSimpleNowInString(String delim) { return toString(getSimpleNow(), delim); } /** * Create String representation of date. * @param date * @return dateStr String representation of date in format MM/dd/YYYY */ public static String toString(Date date) { int[] arrOfDate = createArrayOfDate(date); return String.format("%d/%d/%d", arrOfDate[1], arrOfDate[0], arrOfDate[2]); } /** * Create String representation of date with specified delimeter. * @param date * @param delim * @return dateStr String representation of date in format MM [delim] dd [delim] YYYY */ public static String toString(Date date, String delim) { int[] arrOfDate = createArrayOfDate(date); return String.format("%d%s%d%s%d", arrOfDate[1], delim, arrOfDate[0], delim, arrOfDate[2]); } /** * Return today in date representation, but only containing year, month, and day. * @return today */ public static Date getSimpleNow() { return getSimpleDate(getNow()); } /** * 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; } /** * 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]); } /** * Return today in date representation. Along with hour, minute, second, and other element.<br /> * @return today */ public static Date getNow() { return getCalendar().getTime(); } /** * 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 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); } /** * 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; } }