Class Dates


  • public class Dates
    extends java.lang.Object
    Utilities to working with dates (java.util.Date).

    Author:
    Javier Paniza, Peter Smith
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Dates.DateDistance  
    • Constructor Summary

      Constructors 
      Constructor Description
      Dates()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Dates.DateDistance addDateDistances​(Dates.DateDistance dis1, Dates.DateDistance dis2)  
      static void addDays​(java.util.Date date, int days)
      Add the days to the date.
      static java.util.Date cloneWith2359​(java.util.Date date)
      Returns a clone but with 23:59:59:999 for hours, minutes, seconds and milliseconds.
      static java.sql.Timestamp cloneWith59999​(java.sql.Timestamp date)
      Returns a clone but with 59 seconds and 999 milliseconds.
      static java.sql.Timestamp cloneWith999​(java.sql.Timestamp date)
      Returns a clone but with 999 for milliseconds.
      static java.util.Date cloneWithoutTime​(java.util.Date date)
      Returns a clone but without hours, minutes, seconds and milliseconds.
      static java.util.Date create​(int day, int month, int year)
      With hour to 0.
      static java.util.Date create​(int day, int month, int year, int hourofday, int minute, int second)
      If day, month and year are 0 return null.
      static java.util.Date createCurrent()
      Current date without time.
      static Dates.DateDistance dateDistance​(java.util.Date f1, java.util.Date f2)
      Difference of 2 dates in years, months and days.
      static Dates.DateDistance dateDistance​(java.util.Date f1, java.util.Date f2, boolean includeStartDate)
      Difference of 2 dates in years, months and days.
      static java.lang.String dateFormatForJSCalendar()  
      static java.lang.String dateFormatForJSCalendar​(boolean includeTime)  
      static java.lang.String dateTimeFormatForJSCalendar()  
      static int daysInterval​(java.util.Date startDate, java.util.Date endDate, boolean includeStartDate)
      Returns number of days between startDate and endDate
      static java.util.Date firstOfMonth​(java.util.Date date)
      The date of the first day of month.
      static java.text.DateFormat getDateTimeFormat()
      DateFormat for date + time consistently among all Java versions, according current locale.
      static java.text.DateFormat getDateTimeFormat​(java.util.Locale locale)
      DateFormat for date + time consistently among all Java versions.
      static java.text.DateFormat getDateTimeFormatForParsing​(java.util.Locale locale)
      DateFormat for date + time consistently among all Java versions for using in parsing.
      static int getDay​(java.util.Date date)
      Returns the day of date.
      static java.lang.String getLocalizedDatePattern​(java.util.Locale locale)
      Date partern for short format to be use for both Date and LocalDate.
      static int getMonth​(java.util.Date date)
      Returns the month (1 to 12) of date.
      static int getYear​(java.util.Date date)
      Returns the year (4 digits) of date.
      static boolean hasLocalDate​(java.util.List<MetaProperty> listProperty)
      Used to verify if the entity has dates
      static boolean hasTime​(java.util.Date date)
      If the hours, minutes, seconds and milliseconds are zero.
      static boolean isDifferentDay​(java.util.Date f1, java.util.Date f2)
      Compares if 2 dates are equals at day, month and year level, ignoring time in comparing.
      static java.util.Date lastOfMonth​(java.util.Date date)
      The date of the last day of month.
      static java.util.Date lastOfYear​(java.util.Date date)
      The date of the last day of year.
      static java.util.Date removeTime​(java.util.Date date)
      Puts hours, minutes, seconds and milliseconds to zero.
      static void setDay​(java.util.Date date, int day)
      Put the day to the date.
      static void setMonth​(java.util.Date date, int month)
      Put the month (1 to 12) to the date.
      static void setYear​(java.util.Date date, int year)
      Put the year to the date.
      static Dates.DateDistance subtractDateDistances​(Dates.DateDistance dis1, Dates.DateDistance dis2)  
      static java.sql.Date toSQL​(java.util.Date date)
      Creates a java.sql.Date from a java.util.Date.
      static java.lang.String toString​(java.util.Date date)
      String with date in short format according current locale.
      static java.util.Date withTime​(java.util.Date date)
      Creates a date with day, month and year of original, but with current time.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Dates

        public Dates()
    • Method Detail

      • create

        public static java.util.Date create​(int day,
                                            int month,
                                            int year)
        With hour to 0. If day, month and year are 0 return null.

        The time zone is the default one, so if you print this date in the same JVM you will get the correct one. However, if you move the date object from one machine to other, or save it in a database that will be read from another locale maybe the dates will not match.

      • create

        public static java.util.Date create​(int day,
                                            int month,
                                            int year,
                                            int hourofday,
                                            int minute,
                                            int second)
        If day, month and year are 0 return null.

        The time zone is the default one, so if you print this date in the same JVM you will get the correct one. However, if you move the date object from one machine to other, or save it in a database that will be read from another locale maybe the dates will not match.

      • createCurrent

        public static java.util.Date createCurrent()
        Current date without time.
      • getDay

        public static int getDay​(java.util.Date date)
        Returns the day of date.

        If date is null return 0.

      • getYear

        public static int getYear​(java.util.Date date)
        Returns the year (4 digits) of date. If date is null returns 0.
      • getMonth

        public static int getMonth​(java.util.Date date)
        Returns the month (1 to 12) of date.

        If date is null returns 0.

      • setDay

        public static void setDay​(java.util.Date date,
                                  int day)
        Put the day to the date.

        If date is null it has no effect (but no exception is thrown)

      • setMonth

        public static void setMonth​(java.util.Date date,
                                    int month)
        Put the month (1 to 12) to the date.

        If date is null it has no effect (but no exception is thrown)

      • setYear

        public static void setYear​(java.util.Date date,
                                   int year)
        Put the year to the date.

        If date is null it has no effect (but no exception is thrown)

      • removeTime

        public static java.util.Date removeTime​(java.util.Date date)
        Puts hours, minutes, seconds and milliseconds to zero.

        Returns:
        The same date sent as argument (a new date is not created). If null if sent a null is returned.
      • hasTime

        public static boolean hasTime​(java.util.Date date)
        If the hours, minutes, seconds and milliseconds are zero.

        Returns:
        If argument is null it return false, without throwing exception.
        Since:
        4.8.1
      • cloneWithoutTime

        public static java.util.Date cloneWithoutTime​(java.util.Date date)
        Returns a clone but without hours, minutes, seconds and milliseconds.

        Returns:
        If null if sent a null is returned.
      • cloneWith2359

        public static java.util.Date cloneWith2359​(java.util.Date date)
        Returns a clone but with 23:59:59:999 for hours, minutes, seconds and milliseconds.

        Returns:
        The same date sent as argument (a new date is not created). If null if sent a null is returned.
      • cloneWith999

        public static java.sql.Timestamp cloneWith999​(java.sql.Timestamp date)
        Returns a clone but with 999 for milliseconds.

        Returns:
        The same date sent as argument (a new date is not created). If null if sent a null is returned.
        Since:
        7.1.5
      • cloneWith59999

        public static java.sql.Timestamp cloneWith59999​(java.sql.Timestamp date)
        Returns a clone but with 59 seconds and 999 milliseconds.

        Returns:
        The same date sent as argument (a new date is not created). If null if sent a null is returned.
        Since:
        7.1.6
      • toSQL

        public static java.sql.Date toSQL​(java.util.Date date)
        Creates a java.sql.Date from a java.util.Date.

        Parameters:
        date - If null returns null
      • toString

        public static java.lang.String toString​(java.util.Date date)
        String with date in short format according current locale.

        Current locale is from Locales.getCurrent().

        Parameters:
        date - If null returns empty string
        Returns:
        Not null
      • getDateTimeFormat

        public static java.text.DateFormat getDateTimeFormat()
        DateFormat for date + time consistently among all Java versions, according current locale.

        The date format is consistent among all Java versions, including Java 6, 7, 8, 9, 10 and 11. While standard Java format differently since Java 9. Current locale is from Locales.getCurrent().

        Returns:
        Not null
        Since:
        6.0
      • getDateTimeFormat

        public static java.text.DateFormat getDateTimeFormat​(java.util.Locale locale)
        DateFormat for date + time consistently among all Java versions.

        The date format is consistent among all Java versions, including Java 6, 7, 8, 9, 10 and 11. While standard Java format differently since Java 9. Also uses always 4 digits for year no matter the locale (new in v6.5.2).

        Returns:
        Not null
        Since:
        6.0
      • getDateTimeFormatForParsing

        public static java.text.DateFormat getDateTimeFormatForParsing​(java.util.Locale locale)
        DateFormat for date + time consistently among all Java versions for using in parsing.

        The date format is consistent among all Java versions, including Java 6, 7, 8, 9, 10 and 11. While standard Java format differently since Java 9. It uses 2 or 4 digits for year depending on locale.

        Returns:
        Not null
        Since:
        6.5.2
      • getLocalizedDatePattern

        public static java.lang.String getLocalizedDatePattern​(java.util.Locale locale)
        Date partern for short format to be use for both Date and LocalDate.

        It uses always 4 digits for year.

      • withTime

        public static java.util.Date withTime​(java.util.Date date)
        Creates a date with day, month and year of original, but with current time.

        Parameters:
        date - It is not changed
        Returns:
        If arguments is null then is null
      • isDifferentDay

        public static boolean isDifferentDay​(java.util.Date f1,
                                             java.util.Date f2)
        Compares if 2 dates are equals at day, month and year level, ignoring time in comparing.
        Parameters:
        f1 - Can be null
        f2 - Can be null
      • dateDistance

        public static Dates.DateDistance dateDistance​(java.util.Date f1,
                                                      java.util.Date f2,
                                                      boolean includeStartDate)
        Difference of 2 dates in years, months and days.

        Parameters:
        f1 - If null returns null
        f2 - If null returns null
      • dateDistance

        public static Dates.DateDistance dateDistance​(java.util.Date f1,
                                                      java.util.Date f2)
        Difference of 2 dates in years, months and days.

        Parameters:
        f1 - If null returns null
        f2 - If null returns null
      • dateFormatForJSCalendar

        public static java.lang.String dateFormatForJSCalendar​(boolean includeTime)
        Parameters:
        locale - Removed in 6.5
        Since:
        4.8.1
      • dateFormatForJSCalendar

        public static java.lang.String dateFormatForJSCalendar()
        Parameters:
        locale - Removed in 6.5
      • dateTimeFormatForJSCalendar

        public static java.lang.String dateTimeFormatForJSCalendar()
        Parameters:
        locale - Removed in 6.5
      • daysInterval

        public static int daysInterval​(java.util.Date startDate,
                                       java.util.Date endDate,
                                       boolean includeStartDate)
        Returns number of days between startDate and endDate

        Parameters:
        java - .util.Date startDate
        java - .util.Date endDate
        boolean - includeStartDate

      • addDays

        public static void addDays​(java.util.Date date,
                                   int days)
        Add the days to the date.

        If date is null it has no effect (but no exception is thrown)

        Since:
        4.8.1
      • lastOfYear

        public static java.util.Date lastOfYear​(java.util.Date date)
        The date of the last day of year.

        Parameters:
        The - root date, it is not modified.
        Returns:
        If null if sent a null is returned.
        Since:
        4.8.1
      • lastOfMonth

        public static java.util.Date lastOfMonth​(java.util.Date date)
        The date of the last day of month.

        Parameters:
        The - root date, it is not modified.
        Returns:
        If null if sent a null is returned.
        Since:
        4.8.1
      • firstOfMonth

        public static java.util.Date firstOfMonth​(java.util.Date date)
        The date of the first day of month.

        Parameters:
        The - root date, it is not modified.
        Returns:
        If null if sent a null is returned.
        Since:
        4.8.1
      • hasLocalDate

        public static boolean hasLocalDate​(java.util.List<MetaProperty> listProperty)
        Used to verify if the entity has dates
        Since:
        7.1