Package totalcross.util
Class Date
- java.lang.Object
-
- totalcross.util.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.
-
-
Field Summary
Fields Modifier and Type Field Description static intAPRILstatic intAUGUSTstatic booleanBACKWARDUsed in the advance methods.static intDECEMBERstatic intFEBRUARYstatic booleanFORWARDUsed in the advance methods.static intJANUARYstatic intJULYstatic intJUNEstatic intMARCHstatic intMAYstatic intminValidYearDefines the minimum valid year.static java.lang.String[]monthNamesThe month names used in some routines.static intNOVEMBERstatic intOCTOBERstatic intSEPTEMBERstatic DateSQL_EPOCH
-
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 formatDate(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 voidadvance(int numberDays)Advances the date by a passed integer.voidadvanceMonth()Advances the date to the beginning of the next month.voidadvanceMonth(boolean direction)Advances the date to the beginning of the next or previous month.voidadvanceWeek()Advances the date to the beginning of the next week.voidadvanceWeek(boolean direction)Advances the date to the beginning of the next or previous week.intcompareTo(java.lang.Object other)Implementation of the Comparable interface.booleanequals(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.StringformatDate(int day, int month, int year)Formats the date specified with the Settings.dateFormat, zero padded.static java.lang.StringformatDate(int day, int month, int year, byte dateFormat)Formats the date specified with the dateFormat parameter, zero padded.static java.lang.StringformatDate(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.StringformatDate(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.StringformatDayMonth()Formats the day/month specified with the Settings.dateFormat, zero padded.java.lang.StringgetDate()Deprecated.use toString()intgetDateInt()Returns the date in a integer format.intgetDay()Returns the day.intgetDayOfWeek()Returns the day of week, where 0 is sunday and 6 is saturday.intgetDaysInMonth()Returns number of days in the set month.intgetDaysInMonth(int month)Returns number of days in the passed month of the current date's year.static intgetDaysInMonth(int month, int year)Returns number of days in the passed month and year.intgetGregorianDay()Returns the number of days since the January 1 of the epoch year (1000).intgetMonth()Returns the month.static java.lang.StringgetMonthName(int m)Returns the string representation of the month passedlonggetSQLLong()Returns this date in the Time.getTimeLong format, with hour/minute/second/millis equal to 0.java.lang.StringgetSQLString()Returns this date in the formatYYYY-MM-DD 00:00:00.000java.lang.StringgetSQLString(java.lang.StringBuffer sb)Returns this date in the formatYYYY-MM-DD 00:00:00.000longgetTime()Returns the number of seconds since SQL_EPOCH 1/1/1970.intgetWeek()Calculates and returns the ordinal value of the week(1-52).intgetYear()Returns the year.booleanisAfter(Date sentDate)Checks to see if the Date object passed occurs after the existing Date object.booleanisBefore(Date sentDate)Checks to see if the Date object passed occurs before the existing Date object.static booleanisLeapYear(int year)Checks if the year is a leap yearintset(int day, int month, int year)Sets the date fields to the given ones.intset(java.lang.String strDate, byte dateFormat)Sets the date fields by parsing the given String, and using the dateFormat.voidsetToday()Sets this date object to be the current dayintsubtract(Date other)Returns the difference in days from this date and the given one (other - this).java.lang.StringtoString()Returns the date in a string format.java.lang.StringtoString(byte format)Returns the date as a string, in the given format.java.lang.StringtoString(byte dateFormat, java.lang.String separator)Returns the date in a string format and using the given format and separator.
-
-
-
Field Detail
-
monthNames
public static java.lang.String[] monthNames
The month names used in some routines. You can localize it if you wish.
-
minValidYear
public static int minValidYear
Defines the minimum valid year. Any year before this date throws an InvalidDateException. Defaults to 1000 DC
-
JANUARY
public static final int JANUARY
- See Also:
- Constant Field Values
-
FEBRUARY
public static final int FEBRUARY
- See Also:
- Constant Field Values
-
MARCH
public static final int MARCH
- See Also:
- Constant Field Values
-
APRIL
public static final int APRIL
- See Also:
- Constant Field Values
-
MAY
public static final int MAY
- See Also:
- Constant Field Values
-
JUNE
public static final int JUNE
- See Also:
- Constant Field Values
-
JULY
public static final int JULY
- See Also:
- Constant Field Values
-
AUGUST
public static final int AUGUST
- See Also:
- Constant Field Values
-
SEPTEMBER
public static final int SEPTEMBER
- See Also:
- Constant Field Values
-
OCTOBER
public static final int OCTOBER
- See Also:
- Constant Field Values
-
NOVEMBER
public static final int NOVEMBER
- See Also:
- Constant Field Values
-
DECEMBER
public static final int DECEMBER
- See Also:
- Constant Field Values
-
FORWARD
public static final boolean FORWARD
Used in the advance methods.- See Also:
- Constant Field Values
-
BACKWARD
public static final boolean BACKWARD
Used in the advance methods.- See Also:
- Constant Field Values
-
SQL_EPOCH
public static Date SQL_EPOCH
-
-
Constructor Detail
-
Date
public Date()
Constructs a Date object set to the current date.
-
Date
public Date(Time t) throws InvalidDateException
Constructs a new Date object with the values from the given Time object- Throws:
InvalidDateException
-
Date
public Date(java.lang.String strDate, byte dateFormat) throws InvalidDateExceptionConstructs 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 InvalidDateExceptionConstructs 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 InvalidDateExceptionConstructs 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 InvalidDateExceptionConstructs 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 InvalidDateExceptionSets 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 InvalidDateExceptionSets the date fields by parsing the given String, and using the dateFormat. If you want to use the default date format, useSettings.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, byte dateFormat)Formats the date specified with the dateFormat parameter, zero padded.- See Also:
Settings.DATE_DMY,Settings.DATE_MDY,Settings.DATE_YMD
-
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:
toStringin classjava.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:
equalsin classjava.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:
compareToin interfaceComparable
-
getSQLString
public java.lang.String getSQLString(java.lang.StringBuffer sb)
Returns this date in the formatYYYY-MM-DD 00:00:00.000- Since:
- TotalCross 2.0
-
getSQLString
public java.lang.String getSQLString()
Returns this date in the formatYYYY-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
-
-