Class DateUtils


  • public class DateUtils
    extends java.lang.Object
    Utility methods for Date manipulation.

    This utility class is replaced by CalendarUtils because day related manipulation are meaningfull relative to a Calendar only. Always doing so against the default calendar instance isn't enough. PENDING JW: move the missing ops. Volunteers, please! Once done, this will be deprecated.

    • Constructor Summary

      Constructors 
      Constructor Description
      DateUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long addDays​(long time, int amount)
      Adds amount days to time and returns the resulting time.
      static java.util.Date endOfDay​(java.util.Date date)
      Returns the last millisecond of the specified date.
      static long endOfDayInMillis​(long date)
      Returns the last millisecond of the specified date.
      static int getDayOfWeek​(long date)
      Returns the day of the week.
      static int getDaysDiff​(long t1, long t2)
      Returns the number of days difference between t1 and t2.
      static int getDaysDiff​(long t1, long t2, boolean checkOverflow)
      Returns the number of days difference between t1 and t2.
      static long getEndOfMonth​(long date)
      Returns the date corresponding to the end of the month.
      static long getNextDay​(long date, int startOfWeek)
      Returns the first day after date that has the day of week matching startOfWeek.
      static long getNextMonth​(long date)
      Returns the next month.
      static long getPreviousDay​(long date, int startOfWeek)
      Returns the first day before date that has the day of week matching startOfWeek.
      static long getPreviousMonth​(long date)
      Returns the previous month.
      static long getStartOfMonth​(long date)
      Returns the date corresponding to the start of the month.
      static boolean isFirstOfMonth​(long date)
      Check, whether the date passed in is the first day of the month.
      static boolean isFirstOfYear​(long date)
      Check, whether the date passed in is the first day of the year.
      static long nextDay​(long date)
      Returns the day after date.
      static java.util.Date nextDay​(java.util.Date date)
      Returns the day after date.
      static long nextWeek​(long date)
      Returns the week after date.
      static long previousDay​(long date)
      Returns the day before date.
      static long previousWeek​(long date)
      Returns the week before date.
      static java.util.Date startOfDay​(java.util.Date date)
      Returns a new Date with the hours, milliseconds, seconds and minutes set to 0.
      static long startOfDayInMillis​(long date)
      Returns day in millis with the hours, milliseconds, seconds and minutes set to 0.
      • Methods inherited from class java.lang.Object

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

      • DateUtils

        public DateUtils()
    • Method Detail

      • endOfDay

        public static java.util.Date endOfDay​(java.util.Date date)
        Returns the last millisecond of the specified date.
        Parameters:
        date - Date to calculate end of day from
        Returns:
        Last millisecond of date
      • startOfDay

        public static java.util.Date startOfDay​(java.util.Date date)
        Returns a new Date with the hours, milliseconds, seconds and minutes set to 0.
        Parameters:
        date - Date used in calculating start of day
        Returns:
        Start of date
      • startOfDayInMillis

        public static long startOfDayInMillis​(long date)
        Returns day in millis with the hours, milliseconds, seconds and minutes set to 0.
        Parameters:
        date - long used in calculating start of day
        Returns:
        Start of date
      • endOfDayInMillis

        public static long endOfDayInMillis​(long date)
        Returns the last millisecond of the specified date.
        Parameters:
        date - long to calculate end of day from
        Returns:
        Last millisecond of date
      • nextDay

        public static java.util.Date nextDay​(java.util.Date date)
        Returns the day after date.
        Parameters:
        date - Date used in calculating next day
        Returns:
        Day after date.
      • addDays

        public static long addDays​(long time,
                                   int amount)
        Adds amount days to time and returns the resulting time.
        Parameters:
        time - Base time
        amount - Amount of increment.
        Returns:
        the time + amount days
      • nextDay

        public static long nextDay​(long date)
        Returns the day after date.
        Parameters:
        date - Date used in calculating next day
        Returns:
        Day after date.
      • nextWeek

        public static long nextWeek​(long date)
        Returns the week after date.
        Parameters:
        date - Date used in calculating next week
        Returns:
        week after date.
      • getDaysDiff

        public static int getDaysDiff​(long t1,
                                      long t2,
                                      boolean checkOverflow)
        Returns the number of days difference between t1 and t2.
        Parameters:
        t1 - Time 1
        t2 - Time 2
        checkOverflow - indicates whether to check for overflow
        Returns:
        Number of days between start and end
      • getDaysDiff

        public static int getDaysDiff​(long t1,
                                      long t2)
        Returns the number of days difference between t1 and t2.
        Parameters:
        t1 - Time 1
        t2 - Time 2
        Returns:
        Number of days between start and end
      • isFirstOfYear

        public static boolean isFirstOfYear​(long date)
        Check, whether the date passed in is the first day of the year.
        Parameters:
        date - date to check in millis
        Returns:
        true if date corresponds to the first day of a year
        See Also:
        Date.getTime()
      • isFirstOfMonth

        public static boolean isFirstOfMonth​(long date)
        Check, whether the date passed in is the first day of the month.
        Parameters:
        date - date to check in millis
        Returns:
        true if date corresponds to the first day of a month
        See Also:
        Date.getTime()
      • previousDay

        public static long previousDay​(long date)
        Returns the day before date.
        Parameters:
        date - Date used in calculating previous day
        Returns:
        Day before date.
      • previousWeek

        public static long previousWeek​(long date)
        Returns the week before date.
        Parameters:
        date - Date used in calculating previous week
        Returns:
        week before date.
      • getPreviousDay

        public static long getPreviousDay​(long date,
                                          int startOfWeek)
        Returns the first day before date that has the day of week matching startOfWeek. For example, if you want to find the previous monday relative to date you would call getPreviousDay(date, Calendar.MONDAY).
        Parameters:
        date - Base date
        startOfWeek - Calendar constant correspoding to start of week.
        Returns:
        start of week, return value will have 0 hours, 0 minutes, 0 seconds and 0 ms.
      • getNextDay

        public static long getNextDay​(long date,
                                      int startOfWeek)
        Returns the first day after date that has the day of week matching startOfWeek. For example, if you want to find the next monday relative to date you would call getPreviousDay(date, Calendar.MONDAY).
        Parameters:
        date - Base date
        startOfWeek - Calendar constant correspoding to start of week.
        Returns:
        start of week, return value will have 0 hours, 0 minutes, 0 seconds and 0 ms.
      • getPreviousMonth

        public static long getPreviousMonth​(long date)
        Returns the previous month.
        Parameters:
        date - Base date
        Returns:
        previous month
      • getNextMonth

        public static long getNextMonth​(long date)
        Returns the next month.
        Parameters:
        date - Base date
        Returns:
        next month
      • getStartOfMonth

        public static long getStartOfMonth​(long date)
        Returns the date corresponding to the start of the month.
        Parameters:
        date - Base date
        Returns:
        Start of month.
      • getEndOfMonth

        public static long getEndOfMonth​(long date)
        Returns the date corresponding to the end of the month.
        Parameters:
        date - Base date
        Returns:
        End of month.
      • getDayOfWeek

        public static int getDayOfWeek​(long date)
        Returns the day of the week.
        Parameters:
        date - date
        Returns:
        day of week.