Class DateHelper


  • public final class DateHelper
    extends Object
    Some common methods for date and time.
    Author:
    harald
    • Field Detail

      • MIN_DATE

        public static final Date MIN_DATE
        epochal date zero: 1.1.1970 00:00:00 UTC
      • MIN_LOCALDATE

        public static final LocalDate MIN_LOCALDATE
        epochal date zero: 1.1.1970 00:00:00 UTC
      • MIN_TIMESTAMP

        public static final Timestamp MIN_TIMESTAMP
        epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
      • MIN_LOCALDATETIME

        public static final LocalDateTime MIN_LOCALDATETIME
        epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
    • Method Detail

      • toMillis

        public static long toMillis​(Date date)
        Converts a date to the epochal time in millis with null-check.
        Parameters:
        date - the date, time or timestamp, may be null
        Returns:
        the epochal time in ms, 0 if date is null
      • toDate

        public static Date toDate​(Date date)
        Converts a java.util.Date into an org.tentackle.misc.Date.
        Parameters:
        date - the util date
        Returns:
        the sql date, null if date was null
      • toDate

        public static Date toDate​(Calendar cal)
        Converts a Calendar into a org.tentackle.misc.Date.
        Parameters:
        cal - the calendar
        Returns:
        the date, null if cal was null
      • toTime

        public static Time toTime​(Date date)
        Converts a java.util.Date into a org.tentackle.misc.Time.
        Parameters:
        date - the date
        Returns:
        the time of the day in date, null if date was null
      • toTime

        public static Time toTime​(Calendar cal)
        Converts a Calendar into a org.tentackle.misc.Time.
        Parameters:
        cal - the calendar
        Returns:
        the time of day, null if cal was null
      • toTimestamp

        public static Timestamp toTimestamp​(Date date)
        Converts a java.util.Date into a org.tentackle.misc.Timestamp.
        Parameters:
        date - the date
        Returns:
        the timestamp, null if date was null
      • toTimestamp

        public static Timestamp toTimestamp​(Calendar cal)
        Converts a Calendar into a org.tentackle.misc.Timestamp.
        Parameters:
        cal - the calendar
        Returns:
        the timestamp, null if cal was null
      • today

        public static Calendar today()
        Gets the calendar of the current date and time.
        The calendar is created using the thread-local locale, if set, else the default locale.
        Returns:
        the current calendar instance
      • today

        public static Date today​(int dayOffset,
                                 int monthOffset,
                                 int yearOffset)
        Gets today() with an offset.
        Parameters:
        dayOffset - the offset in days
        monthOffset - the offset in months
        yearOffset - the offset in years
        Returns:
        the date
      • daytime

        public static Time daytime()
        Gets the current time.
        Returns:
        current Time
      • daytime

        public static Time daytime​(int secondOffset,
                                   int minuteOffset,
                                   int hourOffset)
        Gets the current time with an offset.
        Parameters:
        hourOffset - the offset in hours
        minuteOffset - the offset in minutes
        secondOffset - the offset in seconds
        Returns:
        the time
      • now

        public static Timestamp now​(long offsetMillis)
        Gets the current system time plus an optional offset.
        Parameters:
        offsetMillis - the offset to the current system time in milliseconds
        Returns:
        current Timestamp
      • now

        public static Timestamp now()
        Gets the current system time.
        Returns:
        current Timestamp
      • setMidnight

        public static void setMidnight​(Calendar calendar)
        Sets the given calendar to midnight.
        Parameters:
        calendar - the calendar
      • getCurrentYear

        public static int getCurrentYear()
        Gets the current year.
        Returns:
        the current 4-digit year
      • getCurrentMonth

        public static int getCurrentMonth()
        Gets the current month.
        Returns:
        the current month (0 - 11)
      • getCurrentDay

        public static int getCurrentDay()
        Gets the current day.
        Returns:
        the current day (1 - 31)
      • derive4DigitYearFrom2DigitYear

        public static int derive4DigitYearFrom2DigitYear​(int year,
                                                         int currentYear,
                                                         int yearWindow)
        Derives a 4-digit from a 2-digit year.

        Finds the closest distance to the current, past or next century according to a given current year.

        Parameters:
        year - the year, will be cut to 2 digits via modulo 100
        currentYear - the current 4 digit year
        yearWindow - the year window in number of years in the future (0-100, any other value always returns the year in the current century)
        Returns:
        the closest 4-digit year
      • derive4DigitYearFrom2DigitYear

        public static int derive4DigitYearFrom2DigitYear​(int year)
        Derives a 4-digit from a 2-digit year referring to the current year and using a year window of 50.

        Finds the closest distance to the current, past or next century according to the current year.

        Parameters:
        year - the year, will be cut to 2 digits via modulo 100
        Returns:
        the closest 4-digit year
      • deriveYearFromMonth

        public static int deriveYearFromMonth​(int month,
                                              int currentMonth,
                                              int currentYear,
                                              int monthWindow)
        Derives the year from a month.
        Parameters:
        month - the month (0-11)
        currentMonth - the current month (0-11)
        currentYear - the current year (4 digits)
        monthWindow - the monthwindow, (0-11, any other value returns the current year)
        Returns:
        the closest year (4 digits)
      • deriveYearFromMonth

        public static int deriveYearFromMonth​(int month)
        Derives the year from a month referring to the current date and a monthWindow of 6.
        Parameters:
        month - the month (0-11)
        Returns:
        the closest year (4 digits)
      • deriveMonthFromDay

        public static int deriveMonthFromDay​(int day,
                                             int currentDay,
                                             int currentMonth,
                                             int dayWindow)
        Derives the month from a day.
        Parameters:
        day - the day (1-31)
        currentDay - the current day of the month (1-31)
        currentMonth - the current month (0-11)
        dayWindow - the day window (0-30)
        Returns:
        the closest month (0-11 for current year, -1 for december of last year, 12 for january of next year)
      • deriveMonthFromDay

        public static int deriveMonthFromDay​(int day)
        Derives the month from a day referring to the current date and a dayWindow of 15.
        Parameters:
        day - the day (1-31)
        Returns:
        the closest month (0-11 for current year, -1 for december of last year, 12 for january of next year)