Class DateHelper


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

      Fields 
      Modifier and Type Field Description
      static Date MIN_DATE
      epochal date zero: 1.1.1970 00:00:00 UTC
      static java.time.LocalDate MIN_LOCALDATE
      epochal date zero: 1.1.1970 00:00:00 UTC
      static java.time.LocalDateTime MIN_LOCALDATETIME
      epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
      static Timestamp MIN_TIMESTAMP
      epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int convert2DigitYearTo4DigitYear​(int year2)
      Converts a 2-digit year to a 4-digit year.
      static int convert2DigitYearTo4DigitYear​(int year2, int currentYear4)
      Converts a 2-digit year to a 4-digit year.
      static Time daytime()
      Gets the current time.
      static Time daytime​(int secondOffset, int minuteOffset, int hourOffset)
      Gets the current time with an offset.
      static Timestamp now()
      Gets the current system time.
      static Timestamp now​(long offsetMillis)
      Gets the current system time plus an optional offset.
      static void setMidnight​(java.util.GregorianCalendar greg)
      Sets the given calendar to midnight.
      static Date toDate​(java.util.Calendar cal)
      Converts a Calendar into a org.tentackle.misc.Date.
      static Date toDate​(java.util.Date date)
      Converts a java.util.Date into an org.tentackle.misc.Date.
      static Date today()
      Gets the current date.
      static Date today​(int dayOffset, int monthOffset, int yearOffset)
      Gets the current date with an offset.
      static long toMillis​(java.util.Date date)
      Converts a date to the epochal time in millis with null-check.
      static Time toTime​(java.util.Calendar cal)
      Converts a Calendar into a org.tentackle.misc.Time.
      static Time toTime​(java.util.Date date)
      Converts a java.util.Date into a org.tentackle.misc.Time.
      static Timestamp toTimestamp​(java.util.Calendar cal)
      Converts a Calendar into a org.tentackle.misc.Timestamp.
      static Timestamp toTimestamp​(java.util.Date date)
      Converts a java.util.Date into a org.tentackle.misc.Timestamp.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 java.time.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 java.time.LocalDateTime MIN_LOCALDATETIME
        epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
    • Method Detail

      • toMillis

        public static long toMillis​(java.util.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​(java.util.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​(java.util.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​(java.util.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​(java.util.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​(java.util.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​(java.util.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 Date today()
        Gets the current date.
        Returns:
        current Date
      • today

        public static Date today​(int dayOffset,
                                 int monthOffset,
                                 int yearOffset)
        Gets the current date 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​(java.util.GregorianCalendar greg)
        Sets the given calendar to midnight.
        Parameters:
        greg - the calendar
      • convert2DigitYearTo4DigitYear

        public static int convert2DigitYearTo4DigitYear​(int year2,
                                                        int currentYear4)
        Converts a 2-digit year to a 4-digit year.

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

        Parameters:
        year2 - the 2-digit year
        currentYear4 - the current 4 digit year
        Returns:
        the closest 4-digit year
      • convert2DigitYearTo4DigitYear

        public static int convert2DigitYearTo4DigitYear​(int year2)
        Converts a 2-digit year to a 4-digit year.

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

        Parameters:
        year2 - the 2-digit year
        Returns:
        the closest 4-digit year