Class Date

  • All Implemented Interfaces:
    Comparable
    Direct Known Subclasses:
    Timestamp

    public class Date
    extends java.lang.Object
    implements Comparable
    The Date class is a general date data type(Object) that is similar to those built in to other languages. It supports all days from January 1st, 1000 D.C. through December 31st, 2999. It checks to make sure that the dates that are instanciated or changed exist and if they don't an exception is thrown. It provides methods to advance the date backwards and forwards by increments of day, week, and month. It provides comparisons =,>,<. Added by Allan C. Solomon and modified by guich.
    • Constructor Summary

      Constructors 
      Constructor Description
      Date()
      Constructs a Date object set to the current date.
      Date​(int sentDate)
      Constructs a Date object set to the passed int in the YYYYMMDD format
      Date​(int sentDay, int sentMonth, int sentYear)
      Constructs a Date object set to the passed day, month, and year.
      Date​(java.lang.String strDate)
      Constructs a Date object set to a passed string in the format specified in the current date format.
      Date​(java.lang.String strDate, byte dateFormat)
      Constructs a Date object set to a passed string in the format specified in the dateFormat parameter.
      Date​(Time t)
      Constructs a new Date object with the values from the given Time object
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void advance​(int numberDays)
      Advances the date by a passed integer.
      void advanceMonth()
      Advances the date to the beginning of the next month.
      void advanceMonth​(boolean direction)
      Advances the date to the beginning of the next or previous month.
      void advanceWeek()
      Advances the date to the beginning of the next week.
      void advanceWeek​(boolean direction)
      Advances the date to the beginning of the next or previous week.
      int compareTo​(java.lang.Object other)
      Implementation of the Comparable interface.
      boolean equals​(java.lang.Object sentDate)
      Checks to see if the Date object passed occurs at the same time as the existing Date object.
      static java.lang.String formatDate​(int day, int month, int year)
      Formats the date specified with the Settings.dateFormat, zero padded.
      static java.lang.String formatDate​(int day, int month, int year, byte dateFormat)
      Formats the date specified with the dateFormat parameter, zero padded.
      static java.lang.String formatDate​(int day, int month, int year, byte dateFormat, java.lang.String dateSeparator)
      Formats the date specified with the dateFormat parameter, zero padded, and using the given separator.
      static java.lang.String formatDate​(int day, int month, int year, java.lang.String separator)
      Formats the date specified with the Settings.dateFormat, zero padded, and using the given separator.
      java.lang.String formatDayMonth()
      Formats the day/month specified with the Settings.dateFormat, zero padded.
      java.lang.String getDate()
      Deprecated.
      use toString()
      int getDateInt()
      Returns the date in a integer format.
      int getDay()
      Returns the day.
      int getDayOfWeek()
      Returns the day of week, where 0 is sunday and 6 is saturday.
      int getDaysInMonth()
      Returns number of days in the set month.
      int getDaysInMonth​(int month)
      Returns number of days in the passed month of the current date's year.
      static int getDaysInMonth​(int month, int year)
      Returns number of days in the passed month and year.
      int getGregorianDay()
      Returns the number of days since the January 1 of the epoch year (1000).
      int getMonth()
      Returns the month.
      static java.lang.String getMonthName​(int m)
      Returns the string representation of the month passed
      long getSQLLong()
      Returns this date in the Time.getTimeLong format, with hour/minute/second/millis equal to 0.
      java.lang.String getSQLString()
      Returns this date in the format YYYY-MM-DD 00:00:00.000
      java.lang.String getSQLString​(java.lang.StringBuffer sb)
      Returns this date in the format YYYY-MM-DD 00:00:00.000
      long getTime()
      Returns the number of seconds since SQL_EPOCH 1/1/1970.
      int getWeek()
      Calculates and returns the ordinal value of the week(1-52).
      int getYear()
      Returns the year.
      boolean isAfter​(Date sentDate)
      Checks to see if the Date object passed occurs after the existing Date object.
      boolean isBefore​(Date sentDate)
      Checks to see if the Date object passed occurs before the existing Date object.
      static boolean isLeapYear​(int year)
      Checks if the year is a leap year
      int set​(int day, int month, int year)
      Sets the date fields to the given ones.
      int set​(java.lang.String strDate, byte dateFormat)
      Sets the date fields by parsing the given String, and using the dateFormat.
      void setToday()
      Sets this date object to be the current day
      int subtract​(Date other)
      Returns the difference in days from this date and the given one (other - this).
      java.lang.String toString()
      Returns the date in a string format.
      java.lang.String toString​(byte format)
      Returns the date as a string, in the given format.
      java.lang.String toString​(byte dateFormat, java.lang.String separator)
      Returns the date in a string format and using the given format and separator.
      • Methods inherited from class java.lang.Object

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

      • Date

        public Date()
        Constructs a Date object set to the current date.
      • Date

        public Date​(java.lang.String strDate,
                    byte dateFormat)
             throws InvalidDateException
        Constructs a Date object set to a passed string in the format specified in the dateFormat parameter. The constructor auto-detects the separator.
        Parameters:
        strDate - string that should have the format specified in the Settings.dateFormat. Note: does not have to be separated by the '-' character it can be separated by any non-number.
        dateFormat - one of the Settings.DATE_ values.
        Throws:
        InvalidDateException
        See Also:
        Settings.DATE_DMY, Settings.DATE_MDY, Settings.DATE_YMD
      • Date

        public Date​(java.lang.String strDate)
             throws InvalidDateException
        Constructs a Date object set to a passed string in the format specified in the current date format. The constructor auto-detects the separator. If an invalid date is passed, sets to the current date;
        Parameters:
        strDate - string that should have the format specified in the Settings.dateFormat. Note: does not have to be separated by the '-' character it can be separated by any non-number.
        Throws:
        InvalidDateException
        See Also:
        Settings.dateFormat
      • Date

        public Date​(int sentDate)
             throws InvalidDateException
        Constructs a Date object set to the passed int in the YYYYMMDD format
        Parameters:
        sentDate - an integer in the YYYYMMDD format
        Throws:
        InvalidDateException
      • Date

        public Date​(int sentDay,
                    int sentMonth,
                    int sentYear)
             throws InvalidDateException
        Constructs a Date object set to the passed day, month, and year.
        Parameters:
        sentDay - - an integer that must be between 1 and the last day in the month.
        sentMonth - - an integer that must be between 1 and 12.
        sentYear - - an integer that must be between 1000 and 2999.
        Throws:
        InvalidDateException
    • Method Detail

      • set

        public int set​(int day,
                       int month,
                       int year)
                throws InvalidDateException
        Sets the date fields to the given ones.
        Returns:
        The date in the format YYYYMMDD
        Throws:
        InvalidDateException
        Since:
        TotalCross 1.24
      • set

        public int set​(java.lang.String strDate,
                       byte dateFormat)
                throws InvalidDateException
        Sets the date fields by parsing the given String, and using the dateFormat. If you want to use the default date format, use Settings.dateFormat. Trailing spaces are skipped.
        Returns:
        The date in the format YYYYMMDD
        Throws:
        InvalidDateException
        Since:
        TotalCross 1.24
      • setToday

        public void setToday()
        Sets this date object to be the current day
      • getDayOfWeek

        public int getDayOfWeek()
        Returns the day of week, where 0 is sunday and 6 is saturday.
        Returns:
        integer representation of day of week. Integers refer to static constants of day of week.
      • getDay

        public int getDay()
        Returns the day.
        Returns:
        integer value of day.
      • getMonth

        public int getMonth()
        Returns the month.
        Returns:
        integer value of the month.
      • getYear

        public int getYear()
        Returns the year.
        Returns:
        integer value of the year.
      • getWeek

        public int getWeek()
        Calculates and returns the ordinal value of the week(1-52).
        Returns:
        integer representation of ordinal value of the week within the set year.
      • formatDayMonth

        public java.lang.String formatDayMonth()
        Formats the day/month specified with the Settings.dateFormat, zero padded.
      • formatDate

        public static java.lang.String formatDate​(int day,
                                                  int month,
                                                  int year,
                                                  byte dateFormat,
                                                  java.lang.String dateSeparator)
        Formats the date specified with the dateFormat parameter, zero padded, and using the given separator.
        Since:
        TotalCross 1.15
        See Also:
        Settings.DATE_DMY, Settings.DATE_MDY, Settings.DATE_YMD
      • formatDate

        public static java.lang.String formatDate​(int day,
                                                  int month,
                                                  int year,
                                                  java.lang.String separator)
        Formats the date specified with the Settings.dateFormat, zero padded, and using the given separator.
        Since:
        TotalCross 1.15
      • formatDate

        public static java.lang.String formatDate​(int day,
                                                  int month,
                                                  int year)
        Formats the date specified with the Settings.dateFormat, zero padded.
      • getDate

        @Deprecated
        public java.lang.String getDate()
        Deprecated.
        use toString()
        Returns the date in a string format.
        Returns:
        string representation of the date in the current Settings.dateFormat.
      • toString

        public java.lang.String toString()
        Returns the date in a string format.
        Overrides:
        toString in class java.lang.Object
        Returns:
        string representation of the date in the current Settings.dateFormat.
      • toString

        public java.lang.String toString​(byte dateFormat,
                                         java.lang.String separator)
        Returns the date in a string format and using the given format and separator.
        Returns:
        string representation of the date in the current Settings.dateFormat.
        Since:
        TotalCross 1.15
      • toString

        public java.lang.String toString​(byte format)
        Returns the date as a string, in the given format.
        Returns:
        string representation of the date in the current Settings.dateFormat
        Since:
        SuperWaba 5.70.
        See Also:
        Settings.DATE_DMY, Settings.DATE_MDY, Settings.DATE_YMD
      • getDateInt

        public int getDateInt()
        Returns the date in a integer format.
        Returns:
        integer representation of the date (year * 10000) + (month *100) + day
      • getDaysInMonth

        public int getDaysInMonth()
        Returns number of days in the set month.
        Returns:
        integer containing number of days in set month.
      • getDaysInMonth

        public int getDaysInMonth​(int month)
        Returns number of days in the passed month of the current date's year.
        Parameters:
        month - integer between 1 and 12.
        Returns:
        integer containing number of days in passed month.
      • getDaysInMonth

        public static int getDaysInMonth​(int month,
                                         int year)
        Returns number of days in the passed month and year.
        Parameters:
        month - integer between 1 and 12.
        year - integer with the year.
        Returns:
        integer containing number of days in passed month.
      • getMonthName

        public static java.lang.String getMonthName​(int m)
        Returns the string representation of the month passed
        Parameters:
        m - integer between 1 and 12.
        Returns:
        string representation of month passed.
      • isBefore

        public boolean isBefore​(Date sentDate)
        Checks to see if the Date object passed occurs before the existing Date object.
        Parameters:
        sentDate - object to compare with existing.
        Returns:
        boolean stating whether or not it occurs before existing date.
      • isAfter

        public boolean isAfter​(Date sentDate)
        Checks to see if the Date object passed occurs after the existing Date object.
        Parameters:
        sentDate - object to compare with existing.
        Returns:
        boolean stating whether or not it occurs after existing date.
      • equals

        public boolean equals​(java.lang.Object sentDate)
        Checks to see if the Date object passed occurs at the same time as the existing Date object.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        sentDate - object to compare with existing.
        Returns:
        boolean stating whether or not it occurs at the same time as existing date.
      • advanceWeek

        public void advanceWeek()
        Advances the date to the beginning of the next week.
      • advanceWeek

        public void advanceWeek​(boolean direction)
        Advances the date to the beginning of the next or previous week.
        Parameters:
        direction - - static variables FORWARD or BACKWARD instructs the method to either move to the next or previous week
      • advanceMonth

        public void advanceMonth()
        Advances the date to the beginning of the next month.
      • advanceMonth

        public void advanceMonth​(boolean direction)
        Advances the date to the beginning of the next or previous month.
        Parameters:
        direction - - static variables FORWARD or BACKWARD instructs the method to either move to the next or previous month
      • advance

        public void advance​(int numberDays)
        Advances the date by a passed integer.
        Parameters:
        numberDays - integer containing number of days that the date should change can be positive or negative
      • getGregorianDay

        public int getGregorianDay()
        Returns the number of days since the January 1 of the epoch year (1000).
      • subtract

        public int subtract​(Date other)
        Returns the difference in days from this date and the given one (other - this).
        Since:
        TotalCross 1.0
      • isLeapYear

        public static boolean isLeapYear​(int year)
        Checks if the year is a leap year
        Parameters:
        year - year to be checked
        Returns:
        true if the year is a leap year, false otherwise
        Since:
        TotalCross 1.0 beta 4
      • compareTo

        public int compareTo​(java.lang.Object other)
        Implementation of the Comparable interface.
        Specified by:
        compareTo in interface Comparable
      • getSQLString

        public java.lang.String getSQLString​(java.lang.StringBuffer sb)
        Returns this date in the format YYYY-MM-DD 00:00:00.000
        Since:
        TotalCross 2.0
      • getSQLString

        public java.lang.String getSQLString()
        Returns this date in the format YYYY-MM-DD 00:00:00.000
        Since:
        TotalCross 2.0
      • getSQLLong

        public long getSQLLong()
        Returns this date in the Time.getTimeLong format, with hour/minute/second/millis equal to 0.
        Since:
        TotalCross 2.0
      • getTime

        public long getTime()
        Returns the number of seconds since SQL_EPOCH 1/1/1970.
        Since:
        TotalCross 2.1