Package org.kissweb

Class DateUtils

java.lang.Object
org.kissweb.DateUtils

public class DateUtils extends Object
This class deals with dates in several formats including int, Date, Calendar, and LocalDate. int dates are formatted as YYYYMMDD. For example, June 8, 2018 would be represented as 20180608.

All combinations of converting from one type to another are included. In order to reduce the number of methods, most methods deal with the int date type. Given the conversion methods, it is possible to perform any function on any date type.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    addDays(int dt, int n)
    Add N days to a date.
    static int
    addMonths(int dt, int n)
    Add N months to a date.
    static int
    addYears(int dt, int n)
    Add N years to a date.
    static int
    calendar(long d)
    Convert a julian date back into a standard date.
    static int
    day(int dt)
    Returns day portion of a date.
    static int
    dayOfWeek(int dt)
    Returns the day of week number as follows:
    0 = Sunday
    1 = Monday
    2 = Tuesday
    3 = Wednesday
    4 = Thursday
    5 = Friday
    6 = Saturday

    -1 = Invalid date
    static String
    dayOfWeekName(int dt)
    Returns the full name of the week day.
    static String
    Returns the full name of the week day.
    static long
    daysBetween(int dat1, int dat2)
    Return the number of days between two dates.
    static String
    format(String fmt, int dt)
    Formats a date to a string according to a format specification.
    static String
    format2(int dt)
    Format a date with a two digit year as "m/d/yy"
    static String
    format4(int dt)
    Format a date with a four digit year as "m/d/yyyy"
    static String
    formatLong(int dt)
    Format date as "Jan 3, 2018"
    static String
    Format date as "Wed, Jan 3, 2018"
    static String
    formatSQL(int dt)
    Format a date as "YYYY-MM-DD"
    static long
    julian(int dt)
    Convert a date into the number of days since a certain date (julian date)
    static void
    main(String[] argv)
     
    static int
    month(int dt)
    Returns month portion of a date.
    static String
    Returns the full name of the month of the year.
    static int
    parse(String date)
    Parses a string containing a date formatted as follows into an integer representation of that date.
    static Calendar
    toCalendar(int dat)
    convert from an int date to a Calendar date.
    static Calendar
    toCalendar(int y, int m, int d)
    Create a Calendar from a year, month, and day.
    static Calendar
    Convert from a LocalDate date to a Calendar date.
    static Calendar
    Convert from a Date date to a Calendar date.
    static Date
    toDate(int dat)
    convert from an int date to a Date date.
    static Date
    toDate(int y, int m, int d)
    Create a Date from a year, month, and day.
    static Date
    convert from an LocalDate date to a Date date.
    static Date
    convert from an Calendar date to a Date date.
    static int
    Returns the current date as an integer formatted as YYYYMMDD.
    static int
    today(String timeZoneId)
    Returns the current date in the specified time zone as an integer formatted as YYYYMMDD.
    static Calendar
    Return today's date as a Calendar instance.
    static Date
    Return today's date as a Date instance.
    static LocalDate
    Return today's date as a LocalDate instance.
    static int
    toInt(int y, int m, int d)
    Create an int date from a year, month, and day.
    static int
    convert from a LocalDate instance to an int date.
    static int
    convert from a Calendar instance to an int date.
    static int
    toInt(Date dat)
    convert from a Date instance to an int date.
    static LocalDate
    toLocalDate(int dt)
    convert from an int date to a LocalDate date.
    static LocalDate
    toLocalDate(int y, int m, int d)
    Create a LocalDate from a year, month, and day.
    static LocalDate
    Convert from a Calendar date to a LocalDate
    static LocalDate
    Convert from a Date date to a LocalDate date.
    static int
    year(int dt)
    Returns year portion of a date.

    Methods inherited from class java.lang.Object

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

    • DateUtils

      public DateUtils()
  • Method Details

    • today

      public static int today()
      Returns the current date as an integer formatted as YYYYMMDD.
      Returns:
    • today

      public static int today(String timeZoneId)
      Returns the current date in the specified time zone as an integer formatted as YYYYMMDD.
      Parameters:
      timeZoneId - - the time zone to use when computing the current date
      Returns:
      the current date as an integer formatted as YYYYMMDD in the specified time zone
    • todayDate

      public static Date todayDate()
      Return today's date as a Date instance.
      Returns:
    • todayCalendar

      public static Calendar todayCalendar()
      Return today's date as a Calendar instance.
      Returns:
    • todayLocalDate

      public static LocalDate todayLocalDate()
      Return today's date as a LocalDate instance.
      Returns:
    • toInt

      public static int toInt(int y, int m, int d)
      Create an int date from a year, month, and day.
      Parameters:
      y -
      m -
      d -
      Returns:
    • toInt

      public static int toInt(Date dat)
      convert from a Date instance to an int date.
      Parameters:
      dat -
      Returns:
    • toInt

      public static int toInt(Calendar date)
      convert from a Calendar instance to an int date.
      Parameters:
      date -
      Returns:
    • toInt

      public static int toInt(LocalDate dt)
      convert from a LocalDate instance to an int date.
      Parameters:
      dt -
      Returns:
    • toDate

      public static Date toDate(int y, int m, int d)
      Create a Date from a year, month, and day.
      Parameters:
      y -
      m -
      d -
      Returns:
    • toDate

      public static Date toDate(int dat)
      convert from an int date to a Date date.
      Parameters:
      dat - a date formatted as YYYYMMDD
      Returns:
    • toDate

      public static Date toDate(Calendar cal)
      convert from an Calendar date to a Date date.
      Parameters:
      cal -
      Returns:
    • toDate

      public static Date toDate(LocalDate dt)
      convert from an LocalDate date to a Date date.
      Parameters:
      dt -
      Returns:
    • toCalendar

      public static Calendar toCalendar(int y, int m, int d)
      Create a Calendar from a year, month, and day.
      Parameters:
      y -
      m -
      d -
      Returns:
    • toCalendar

      public static Calendar toCalendar(int dat)
      convert from an int date to a Calendar date.
      Parameters:
      dat -
      Returns:
    • toCalendar

      public static Calendar toCalendar(Date date)
      Convert from a Date date to a Calendar date.
      Parameters:
      date -
      Returns:
    • toCalendar

      public static Calendar toCalendar(LocalDate dt)
      Convert from a LocalDate date to a Calendar date.
      Parameters:
      dt -
      Returns:
    • toLocalDate

      public static LocalDate toLocalDate(int y, int m, int d)
      Create a LocalDate from a year, month, and day.
      Parameters:
      y -
      m -
      d -
      Returns:
    • toLocalDate

      public static LocalDate toLocalDate(int dt)
      convert from an int date to a LocalDate date.
      Parameters:
      dt - a date formatted as YYYYMMDD
      Returns:
    • toLocalDate

      public static LocalDate toLocalDate(Date date)
      Convert from a Date date to a LocalDate date.
      Parameters:
      date -
      Returns:
    • toLocalDate

      public static LocalDate toLocalDate(Calendar dt)
      Convert from a Calendar date to a LocalDate
      Parameters:
      dt - a Calendar object
      Returns:
    • year

      public static int year(int dt)
      Returns year portion of a date.
      Parameters:
      dt - YYYYMMDD or YYMMDD
      Returns:
      YYYY
    • month

      public static int month(int dt)
      Returns month portion of a date.
      Parameters:
      dt - YYYYMMDD
      Returns:
      MM
    • day

      public static int day(int dt)
      Returns day portion of a date.
      Parameters:
      dt - YYYYMMDD
      Returns:
      DD
    • addDays

      public static int addDays(int dt, int n)
      Add N days to a date.
      Parameters:
      dt - a date formatted as YYYYMMDD
      n - the number of days to add, may be negative
      Returns:
      See Also:
    • addMonths

      public static int addMonths(int dt, int n)
      Add N months to a date.
      Parameters:
      dt - a date formatted as YYYYMMDD
      n -
      Returns:
      See Also:
    • addYears

      public static int addYears(int dt, int n)
      Add N years to a date.
      Parameters:
      dt - a date formatted as YYYYMMDD
      n -
      Returns:
      See Also:
    • format

      public static String format(String fmt, int dt)
      Formats a date to a string according to a format specification.
      Parameters:
      fmt - format specification
      dt - a date formatted as YYYYMMDD
      Returns:
      See Also:
    • format2

      public static String format2(int dt)
      Format a date with a two digit year as "m/d/yy"
      Parameters:
      dt - a date formatted as YYYYMMDD
      Returns:
    • format4

      public static String format4(int dt)
      Format a date with a four digit year as "m/d/yyyy"
      Parameters:
      dt - a date formatted as YYYYMMDD
      Returns:
    • formatSQL

      public static String formatSQL(int dt)
      Format a date as "YYYY-MM-DD"
      Parameters:
      dt - a date formatted as YYYYMMDD
      Returns:
    • formatLong

      public static String formatLong(int dt)
      Format date as "Jan 3, 2018"
      Parameters:
      dt - a date formatted as YYYYMMDD
      Returns:
      See Also:
    • formatLongWithWeekDay

      public static String formatLongWithWeekDay(int dt)
      Format date as "Wed, Jan 3, 2018"
      Parameters:
      dt - a date formatted as YYYYMMDD
      Returns:
    • daysBetween

      public static long daysBetween(int dat1, int dat2)
      Return the number of days between two dates. (dat1 - dat2)
      Parameters:
      dat1 -
      dat2 -
      Returns:
    • parse

      public static int parse(String date)
      Parses a string containing a date formatted as follows into an integer representation of that date.

      MM/DD/YY MM/DD/YYYY MM.DD.YYYY YYYY-MM-DD
      Parameters:
      date - input date
      Returns:
      int date or 0 on error
    • julian

      public static long julian(int dt)
      Convert a date into the number of days since a certain date (julian date)
      Parameters:
      dt - YYYYMMDD
      Returns:
      number of days since some early start date
    • calendar

      public static int calendar(long d)
      Convert a julian date back into a standard date.
      Parameters:
      d -
      Returns:
      YYYYMMDD
    • dayOfWeek

      public static int dayOfWeek(int dt)
      Returns the day of week number as follows:
      0 = Sunday
      1 = Monday
      2 = Tuesday
      3 = Wednesday
      4 = Thursday
      5 = Friday
      6 = Saturday

      -1 = Invalid date
      Parameters:
      dt - YYYYMMDD
      Returns:
    • dayOfWeekName

      public static String dayOfWeekName(int dt)
      Returns the full name of the week day.
      Parameters:
      dt - YYYYMMDD
      Returns:
    • dayOfWeekName

      public static String dayOfWeekName(Date dt)
      Returns the full name of the week day.
      Parameters:
      dt -
      Returns:
    • monthOfYearName

      public static String monthOfYearName(int dt)
      Returns the full name of the month of the year.
      Parameters:
      dt - YYYYMMDD
      Returns:
    • main

      public static void main(String[] argv)