Class DateUtil

java.lang.Object
org.nuiton.util.DateUtil

public class DateUtil
extends java.lang.Object
Library for manipulating dates.

Note:

Since:
1.4.1
Author:
fdesbois
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String DEFAULT_PATTERN  
    static java.lang.String MONTH_PATTERN  
  • Constructor Summary

    Constructors 
    Constructor Description
    DateUtil()  
  • Method Summary

    Modifier and Type Method Description
    static boolean between​(java.util.Date myDate, java.util.Date beforeDate, java.util.Date afterDate)
    Check if the first date in argument is included between the two other dates.
    static java.util.Date createDate​(int dd, int mm, int yy)
    Create a new date from day, month and year (French version).
    static java.util.Date createDate​(int s, int m, int h, int dd, int mm, int yy)
    Create a new date from day, month and year (French version).
    static java.util.Date createDateAfterToday​(int ddStep, int mmStep, int yyStep)
    Create a new date after the current date (today) with modification on day, month and year.
    static boolean currentPeriod​(java.util.Date beforeDate, java.util.Date afterDate)
    Check if the current date is between the two dates in argument.
    static java.lang.String formatDate​(java.util.Date date, java.lang.String pattern)
    Format a date using the pattern in argument.
    static java.lang.String formatDate​(java.util.Date date, java.lang.String pattern, java.util.Locale locale)  
    static int getAge​(java.util.Date birthDate)
    Get the age of a person born on the date in argument.
    static java.util.Date getDateAndTime​(java.util.Date day, java.util.Date time, boolean useSecond, boolean useMiliSecond)
    Créer la date qui utilise le jour donné dans day et l'heure donnée dans time.
    static java.util.Date getDay​(java.util.Date date)
    Enleve les données des heures (hour, minute, second, milli = 0).
    static java.util.Calendar getDefaultCalendar​(java.util.Date date)
    Get the calendar corresponding to the date.
    static int getDifferenceInDays​(java.util.Date beginDate, java.util.Date endDate)
    Do the difference between the two dates in argument.
    static int getDifferenceInHours​(java.util.Date beginDate, java.util.Date endDate)
    Do the difference between the two dates in argument.
    static int getDifferenceInMinutes​(java.util.Date beginDate, java.util.Date endDate)
    Do the difference between the two dates in argument.
    static int getDifferenceInMonths​(java.util.Date beginDate, java.util.Date endDate)
    Do the difference between the two dates in argument.
    static int getDifferenceInSeconds​(java.util.Date beginDate, java.util.Date endDate)
    Do the difference between the two dates in argument.
    static java.util.Date getEndOfDay​(java.util.Date date)
    Positionne une date sur la fin d'un jour
    static int getMonth​(java.util.Date date)
    Get the month value from a date (between 0 and 11).
    static java.lang.String getMonthLibelle​(int monthNumber)
    Get libelle of the month corresponding to the number given in argument.
    static java.lang.String getMonthLibelle​(int monthNumber, java.util.Locale locale)
    Get libelle of the month corresponding to the number given in argument.
    static java.util.Date getTime​(java.util.Date dayTime, boolean useSecond, boolean useMiliSecond)
    Créer la date qui utilise uniquement l'heure donnée dans dayTime.
    static java.util.Date getYesterday​(java.util.Date date)
    Get the date before today
    static java.util.Date parseDate​(java.lang.String date, java.lang.String pattern)
    Parse a date using the pattern in argument.
    static java.util.Date setFirstDayOfMonth​(java.util.Date date)
    Set the first day of month to the date in argument.
    static java.util.Date setFirstDayOfYear​(java.util.Date date)
    Set the first day of year to the date in argument.
    static java.util.Date setLastDayOfMonth​(java.util.Date date)
    Set the last day of month to the date in argument.
    static java.util.Date setLastDayOfYear​(java.util.Date date)
    Set the last day of year to the date in argument.
    static java.util.Date setMaxTimeOfDay​(java.util.Date date)
    Set the max time of the day.
    static java.util.Date setMinTimeOfDay​(java.util.Date date)
    Set the min time of the day : 00:00:00.000.
    static java.util.Date truncateToDayOfWeek​(java.util.Date date)
    Truncate a date to its week (to monday).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • formatDate

      public static java.lang.String formatDate​(java.util.Date date, java.lang.String pattern)
      Format a date using the pattern in argument. The pattern is the same using for DateFormat object.
      Parameters:
      date - the date to format
      pattern - the pattern to use
      Returns:
      a String corresponding to the date formatted
      See Also:
      DateFormat
    • formatDate

      public static java.lang.String formatDate​(java.util.Date date, java.lang.String pattern, java.util.Locale locale)
    • parseDate

      public static java.util.Date parseDate​(java.lang.String date, java.lang.String pattern) throws java.text.ParseException
      Parse a date using the pattern in argument. The pattern is the same using for DateFormat object.
      Parameters:
      date - the String to parse
      pattern - the pattern to use
      Returns:
      a Date corresponding to the String argument parsed
      Throws:
      java.text.ParseException - for parsing errors
      See Also:
      DateFormat
    • createDate

      public static java.util.Date createDate​(int s, int m, int h, int dd, int mm, int yy)
      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.
      Parameters:
      s - value of the seconds 1-60
      m - value of the minutes 1-60
      h - value of the hours 1-24
      dd - value of the day 1-31
      mm - value of the month 1-12
      yy - value of the year 0-9999
      Returns:
      a new date
    • createDate

      public static java.util.Date createDate​(int dd, int mm, int yy)
      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
      Parameters:
      dd - value of the day 1-31
      mm - value of the month 1-12
      yy - value of the year 0-9999
      Returns:
      a new date
    • createDateAfterToday

      public static java.util.Date createDateAfterToday​(int ddStep, int mmStep, int yyStep)
      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.
      Parameters:
      ddStep - nb days you want to increase from the current date
      mmStep - nb months you want to increase from the current date
      yyStep - nb years you want to increase from the current date
      Returns:
      a new date from the current date increase by days, months and years.
    • setLastDayOfMonth

      public static java.util.Date setLastDayOfMonth​(java.util.Date date)
      Set the last day of month to the date in argument. The value depends on the month of the date. (30 april, 28 february, ...)
      Parameters:
      date - Date to modify
      Returns:
      the date with day of month modified
    • setFirstDayOfMonth

      public static java.util.Date setFirstDayOfMonth​(java.util.Date date)
      Set the first day of month to the date in argument.
      Parameters:
      date - Date to modify
      Returns:
      the date with day of month modified
    • setLastDayOfYear

      public static java.util.Date setLastDayOfYear​(java.util.Date date)
      Set the last day of year to the date in argument.
      Parameters:
      date - Date to modify
      Returns:
      the date with day of year modified
    • setFirstDayOfYear

      public static java.util.Date setFirstDayOfYear​(java.util.Date date)
      Set the first day of year to the date in argument.
      Parameters:
      date - Date to modify
      Returns:
      the date with day of year modified
    • setMinTimeOfDay

      public static java.util.Date setMinTimeOfDay​(java.util.Date date)
      Set the min time of the day : 00:00:00.000.
      Parameters:
      date - to modify
      Returns:
      Date with the time set to the minimum in the day
    • setMaxTimeOfDay

      public static java.util.Date setMaxTimeOfDay​(java.util.Date date)
      Set the max time of the day. EUROPE : 23:59:59.999.
      Parameters:
      date - to modify
      Returns:
      Date with the time set to the maximum in the day
    • between

      public static boolean between​(java.util.Date myDate, java.util.Date beforeDate, java.util.Date afterDate)
      Check if the first date in argument is included between the two other dates. The argument myDate can be equals to beforeDate or afterDate to validate the includes.
      Parameters:
      myDate - the date to test
      beforeDate - the first date of the period to test
      afterDate - the second date of the period to test
      Returns:
      true if myDate is included between beforeDate and afterDate
    • currentPeriod

      public static boolean currentPeriod​(java.util.Date beforeDate, java.util.Date afterDate)
      Check if the current date is between the two dates in argument.
      Parameters:
      beforeDate - the first date of the period
      afterDate - the second date of the period
      Returns:
      true if the current date is included between the two dates, false otherwise
      See Also:
      between(Date, Date, Date)
    • getMonth

      public static int getMonth​(java.util.Date date)
      Get the month value from a date (between 0 and 11).
      Parameters:
      date - the date to extract month
      Returns:
      the month value of the date
    • getDifferenceInSeconds

      public static int getDifferenceInSeconds​(java.util.Date beginDate, java.util.Date endDate)
      Do the difference between the two dates in argument. The result is a number of seconds between the two dates.
      Parameters:
      beginDate - first date
      endDate - second date
      Returns:
      a number of seconds between beginDate and endDate
    • getDifferenceInMinutes

      public static int getDifferenceInMinutes​(java.util.Date beginDate, java.util.Date endDate)
      Do the difference between the two dates in argument. The result is a number of minutes between the two dates.
      Parameters:
      beginDate - first date
      endDate - second date
      Returns:
      a number of minutes between beginDate and endDate
    • getDifferenceInHours

      public static int getDifferenceInHours​(java.util.Date beginDate, java.util.Date endDate)
      Do the difference between the two dates in argument. The result is a number of hours between the two dates.
      Parameters:
      beginDate - first date
      endDate - second date
      Returns:
      a number of hours between beginDate and endDate
    • getDifferenceInDays

      public static int getDifferenceInDays​(java.util.Date beginDate, java.util.Date endDate)
      Do the difference between the two dates in argument. The result is a number of days between the two dates. Ex : 28/01/2009 and 08/02/2009 return 11.
      Parameters:
      beginDate - first date
      endDate - second date
      Returns:
      a number of days between beginDate and endDate
    • getDifferenceInMonths

      public static int getDifferenceInMonths​(java.util.Date beginDate, java.util.Date endDate)
      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
      Parameters:
      beginDate - first date
      endDate - second date
      Returns:
      a number of months between beginDate and endDate
    • getAge

      public static int getAge​(java.util.Date birthDate)
      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).
      Parameters:
      birthDate - birth date
      Returns:
      a number of years between birthDate and now
    • getMonthLibelle

      public static java.lang.String getMonthLibelle​(int monthNumber, java.util.Locale locale)
      Get libelle of the month corresponding to the number given in argument.
      Parameters:
      monthNumber - between 1-12
      locale - Locale for language support
      Returns:
      a String corresponding to the libelle of the month
    • getMonthLibelle

      public static java.lang.String getMonthLibelle​(int monthNumber)
      Get libelle of the month corresponding to the number given in argument.
      Parameters:
      monthNumber - between 1-12
      Returns:
      a String corresponding to the libelle of the month
    • getYesterday

      public static java.util.Date getYesterday​(java.util.Date date)
      Get the date before today
      Parameters:
      date - concerned
      Returns:
      Date before today
    • getDefaultCalendar

      public static java.util.Calendar getDefaultCalendar​(java.util.Date date)
      Get the calendar corresponding to the date. The default calendar will be returned (default time zone and locale).
      Parameters:
      date - used to set the calendar time
      Returns:
      the default calendar with time corresponding to the date
    • truncateToDayOfWeek

      public static java.util.Date truncateToDayOfWeek​(java.util.Date date)
      Truncate a date to its week (to monday). It's equivalent to a call to DateUtils.truncate(Date, int) and giving Calendar.DAY_OF_WEEK argument, but such a call raise an exception showing that this field is not supported. This method allows you to bypass this limitation.
      Parameters:
      date - any date
      Returns:
      a date in the same week as given date, a monday. All field below (hours, secondes, ms) are zeroed.
    • getDay

      public static java.util.Date getDay​(java.util.Date date)
      Enleve les données des heures (hour, minute, second, milli = 0).
      Parameters:
      date - la date a modifier
      Returns:
      la date d'un jour
    • getEndOfDay

      public static java.util.Date getEndOfDay​(java.util.Date date)
      Positionne une date sur la fin d'un jour
      Parameters:
      date - la date a modifier
      Returns:
      la date d'un jour
    • getDateAndTime

      public static java.util.Date getDateAndTime​(java.util.Date day, java.util.Date time, boolean useSecond, boolean useMiliSecond)
      Créer la date qui utilise le jour donné dans day et l'heure donnée dans time.
      Parameters:
      day - le jour à utiliser
      time - l'heure a utiliser
      useSecond - FIXME
      useMiliSecond - FIXME
      Returns:
      la date donnée avec l'heure courante
    • getTime

      public static java.util.Date getTime​(java.util.Date dayTime, boolean useSecond, boolean useMiliSecond)
      Créer la date qui utilise uniquement l'heure donnée dans dayTime.
      Parameters:
      dayTime - l'heure a utiliser
      useSecond - FIXME
      useMiliSecond - FIXME
      Returns:
      la date donnée avec uniquement l'heure courante