Class DateHelper

java.lang.Object
org.tentackle.common.DateHelper

public final class DateHelper extends Object
Some common methods for date- and time-related types based on Date.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Date
    epochal date zero: 1.1.1970 00:00:00 GMT
    static final Timestamp
    epochal timestamp zero: 1.1.1970 00:00:00.000 GMT
  • Method Summary

    Modifier and Type
    Method
    Description
    static Time
    Gets the current time.
    static Time
    daytime(int secondOffset, int minuteOffset, int hourOffset)
    Gets the current time with an offset.
    static int
    Derives a 4-digit from a 2-digit year referring to the current year and using a year window of 50.
    static int
    derive4DigitYearFrom2DigitYear(int year, int currentYear, int yearWindow)
    Derives a 4-digit from a 2-digit year.
    static int
    Derives the month from a day referring to the current date and a dayWindow of 15.
    static int
    deriveMonthFromDay(int day, int currentDay, int currentMonth, int dayWindow)
    Derives the month from a day.
    static int
    Derives the year from a month referring to the current date and a monthWindow of 6.
    static int
    deriveYearFromMonth(int month, int currentMonth, int currentYear, int monthWindow)
    Derives the year from a month.
    static int
    Gets the current day.
    static int
    Gets the current month.
    static int
    Gets the current year.
    static Timestamp
    now()
    Gets the current system time.
    static Timestamp
    now(long offsetMillis)
    Gets the current system time plus an optional offset.
    static void
    Sets the given calendar to midnight.
    static Calendar
    Creates a calendar for the given date.
    The calendar is created using the thread-local locale, if set, else the default locale.
    static Calendar
    Creates a calendar for the given time.
    The calendar is created using the thread-local locale, if set, else the default locale.
    static Calendar
    toCalendar(Timestamp timestamp)
    Creates a calendar for the given timestamp.
    The calendar is created using the thread-local locale, if set, else the default locale.
    static Date
    Converts a Calendar into a org.tentackle.misc.Date.
    static Date
    toDate(Date date)
    Converts a java.util.Date into an org.tentackle.misc.Date.
    static Calendar
    Creates a calendar of the current date and time.
    The calendar is created using the thread-local locale, if set, else the default locale.
    static Date
    today(int dayOffset, int monthOffset, int yearOffset)
    Gets today() with an offset.
    static long
    toMillis(Date date)
    Converts a date to the epochal time in millis with null-check.
    static Time
    Converts a Calendar into a org.tentackle.misc.Time.
    static Time
    toTime(Date date)
    Converts a java.util.Date into a org.tentackle.misc.Time.
    static Timestamp
    Converts a Calendar into a org.tentackle.misc.Timestamp.
    static Timestamp
    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 Details

    • MIN_DATE

      public static final Date MIN_DATE
      epochal date zero: 1.1.1970 00:00:00 GMT
    • MIN_TIMESTAMP

      public static final Timestamp MIN_TIMESTAMP
      epochal timestamp zero: 1.1.1970 00:00:00.000 GMT
  • Method Details

    • 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()
      Creates a 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
    • toCalendar

      public static Calendar toCalendar(Date date)
      Creates a calendar for the given date.
      The calendar is created using the thread-local locale, if set, else the default locale.
      Parameters:
      date - the date
      Returns:
      the calendar representing the date
    • toCalendar

      public static Calendar toCalendar(Timestamp timestamp)
      Creates a calendar for the given timestamp.
      The calendar is created using the thread-local locale, if set, else the default locale.

      Notice: org.tentackle.common.Timestamp does not extend org.tentackle.common.Date!

      Parameters:
      timestamp - the timestamp
      Returns:
      the calendar representing the timestamp
    • toCalendar

      public static Calendar toCalendar(Time time)
      Creates a calendar for the given time.
      The calendar is created using the thread-local locale, if set, else the default locale.

      Notice: org.tentackle.common.Time does not extend org.tentackle.common.Date!

      Parameters:
      time - the time
      Returns:
      the calendar representing the time
    • 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:
      secondOffset - the offset in seconds
      minuteOffset - the offset in minutes
      hourOffset - the offset in hours
      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 month window, (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)