/** * Check if the current date is between the two dates in argument. * * @param beforeDate the first date of the period * @param afterDate the second date of the period * @return true if the current date is included between the two dates, * false otherwise * @see #between(Date, Date, Date) */ public static boolean currentPeriod(Date beforeDate, Date afterDate) { return between(new Date(), beforeDate, afterDate); }
/** * Get the month value from a date (between 0 and 11). * * @param date the date to extract month * @return the month value of the date */ public static int getMonth(Date date) { Calendar calendar = getDefaultCalendar(date); return calendar.get(Calendar.MONTH); }
/** * Get libelle of the month corresponding to the number given in argument. * * @param monthNumber between 1-12 * @return a String corresponding to the libelle of the month */ public static String getMonthLibelle(int monthNumber) { return getMonthLibelle(monthNumber, Locale.getDefault()); }
/** * Create a new date from day, month and year (French version). * The month is the real month of the year and not the one which is stored * in Calendar object. Time is set to 00:00:00.000 * * @param dd value of the day 1-31 * @param mm value of the month 1-12 * @param yy value of the year 0-9999 * @return a new date */ public static Date createDate(int dd, int mm, int yy) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, yy); calendar.set(Calendar.MONTH, mm - 1); calendar.set(Calendar.DAY_OF_MONTH, dd); return setMinTimeOfDay(calendar.getTime()); }
/** * Set the first day of month to the date in argument. * * @param date Date to modify * @return the date with day of month modified */ public static Date setFirstDayOfMonth(Date date) { Calendar calendar = getDefaultCalendar(date); calendar.set(Calendar.DAY_OF_MONTH, 1); Date firstDay = calendar.getTime(); return firstDay; }
/** * Set the first day of year to the date in argument. * * @param date Date to modify * @return the date with day of year modified */ public static Date setFirstDayOfYear(Date date) { Calendar calendar = getDefaultCalendar(date); calendar.set(Calendar.DAY_OF_YEAR, 1); Date firstDay = calendar.getTime(); return firstDay; }
/** * Set the last day of year to the date in argument. * * @param date Date to modify * @return the date with day of year modified */ public static Date setLastDayOfYear(Date date) { Calendar calendar = getDefaultCalendar(date); int maximum = calendar.getActualMaximum(Calendar.DAY_OF_YEAR); calendar.set(Calendar.DAY_OF_YEAR, maximum); Date lastDay = calendar.getTime(); return lastDay; }
/** * Do the difference between the two dates in argument. The result is a number * of months between the two dates. * Ex : 01/01/2009 and 28/02/2009 return 2 months. * Warning, if beginDate is inferior to endDate, the result will be 1 minimum * * @param beginDate first date * @param endDate second date * @return a number of months between beginDate and endDate */ public static int getDifferenceInMonths(Date beginDate, Date endDate) { int count = 0; Calendar fromCalendar = getDefaultCalendar(beginDate); Calendar thruCalendar = getDefaultCalendar(endDate); while (fromCalendar.before(thruCalendar)) { fromCalendar.add(Calendar.MONTH, 1); count++; } return count; }
/** * Set the last day of month to the date in argument. * The value depends on the month of the date. (30 april, 28 february, ...) * * @param date Date to modify * @return the date with day of month modified */ public static Date setLastDayOfMonth(Date date) { Calendar calendar = getDefaultCalendar(date); int maximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, maximum); Date lastDay = calendar.getTime(); return lastDay; }
/** * Create a new date after the current date (today) with modification on day, month and year. * You can use negative values on arguments to have a date before today. * * @param ddStep nb days you want to increase from the current date * @param mmStep nb months you want to increase from the current date * @param yyStep nb years you want to increase from the current date * @return a new date from the current date increase by days, months and years. */ public static Date createDateAfterToday(int ddStep, int mmStep, int yyStep) { Calendar calendar = getDefaultCalendar(new Date()); calendar.add(Calendar.DAY_OF_MONTH, ddStep); calendar.add(Calendar.MONTH, mmStep); calendar.add(Calendar.YEAR, yyStep); return calendar.getTime(); }
/** * Get the age of a person born on the date in argument. The result is a number * of years between the birth date and now. * Ex : 01/01/2000 returns 11 years (now date is 26/10/2011). * * @param birthDate birth date * @return a number of years between birthDate and now */ public static int getAge(Date birthDate) { int count = 0; Calendar fromCalendar = getDefaultCalendar(birthDate); Calendar thruCalendar = getDefaultCalendar(new Date()); fromCalendar.add(Calendar.YEAR, 1); while (fromCalendar.before(thruCalendar)) { count++; fromCalendar.add(Calendar.YEAR, 1); } return count; }
/** * Set the min time of the day : 00:00:00.000. * * @param date to modify * @return Date with the time set to the minimum in the day */ public static Date setMinTimeOfDay(Date date) { Calendar calendar = getDefaultCalendar(date); calendar.set(Calendar.AM_PM, Calendar.AM); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); }
/** * Set the max time of the day. EUROPE : 23:59:59.999. * * @param date to modify * @return Date with the time set to the maximum in the day */ public static Date setMaxTimeOfDay(Date date) { Calendar calendar = getDefaultCalendar(date); calendar.set(Calendar.AM_PM, Calendar.PM); calendar.set(Calendar.HOUR, 11); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); }
/** * Get the date before today * * @param date concerned * @return Date before today */ public static Date getYesterday(Date date) { Calendar cal = getDefaultCalendar(date); if (cal.get(Calendar.MONTH) == Calendar.JANUARY && cal.get(Calendar.DAY_OF_MONTH) == 1) { cal.roll(Calendar.YEAR, false); } if (cal.get(Calendar.DAY_OF_MONTH) == 1) { cal.roll(Calendar.MONTH, false); } cal.roll(Calendar.DAY_OF_MONTH, false); return cal.getTime(); }