Date Class

Used to parse, format, and compute dates

Constructors

Date( )
Date( java.util.Date date )
Date( Calendar calendar )
Date( java.time.LocalDate date )
Date( long milliseconds )
Date( String date )
Date( String date, String format )

Properties

INTERVAL_MILLISECONDS
INTERVAL_SECONDS
INTERVAL_MINUTES
INTERVAL_HOURS
INTERVAL_DAYS
INTERVAL_WEEKS
INTERVAL_MONTHS
INTERVAL_YEARS

Public Methods

setDate( String date, String format ) returns javaxt.utils.Date
Used to update the current date using a date string. The format parameter is used to create a SimpleDateFormat to parse the input date string.
setDate( java.util.Date date ) returns javaxt.utils.Date
Used to update the current date using a predefined java.util.Date
setLocale( Locale locale ) returns javaxt.utils.Date
Used to update the current local
getLocale( ) returns Locale
Returns the current local
setTimeZone( String timeZone, boolean preserveTimeStamp ) returns javaxt.utils.Date
Used to set the current time zone. The time zone is used when comparing and formatting dates.
timeZoneName of the time zone (e.g. "UTC", "EDT", etc.)
preserveTimeStampFlag used to indicate whether to preserve the timestamp when changing time zones. Normally, when updating the timezone, the timestamp is updated to the new timezone. For example, if the current time is 4PM EST and you wish to switch to UTC, the timestamp would be updated to 8PM. The preserveTimeStamp flag allows users to preserve the the timestamp so that the timestamp remains fixed at 4PM.
setTimeZone( java.util.TimeZone timeZone, boolean preserveTimeStamp ) returns javaxt.utils.Date
Used to set the current time zone. The time zone is used when comparing and formatting dates.
timeZoneTime zone (e.g. "UTC", "EDT", etc.)
preserveTimeStampFlag used to indicate whether to preserve the timestamp when changing time zones. Normally, when updating the timezone, the timestamp is updated to the new timezone. For example, if the current time is 4PM EST and you wish to switch to UTC, the timestamp would be updated to 8PM. The preserveTimeStamp flag allows users to preserve the the timestamp so that the timestamp remains fixed at 4PM.
setTimeZone( String timeZone ) returns javaxt.utils.Date
Used to set the current time zone. The time zone is used when comparing and formatting dates.
timeZoneName of the time zone (e.g. "UTC", "EST", etc.)
setTimeZone( java.util.TimeZone timeZone ) returns javaxt.utils.Date
Used to set the current time zone. The time zone is used when comparing and formatting dates.
getTimeZone( ) returns java.util.TimeZone
Returns the current time zone. The time zone is used when comparing and formatting dates.
hashCode( ) returns int
toString( ) returns String
Returns the current date as a String in the following format: "EEE MMM dd HH:mm:ss z yyyy"
toString( String format ) returns String
Used to format the current date into a string.
formatPattern used to format the date (e.g. "MM/dd/yyyy hh:mm a", "EEE MMM dd HH:mm:ss z yyyy", etc). Please refer to the java.text.SimpleDateFormat class for more information.
toString( String format, String timeZone ) returns String
Used to format the current date into a string in a given timezone.
format
timeZoneName of the time zone (e.g. "UTC", "EST", etc.). Note that this parameter does not alter the current date in any way. This parameter is simply used for the output string. Use the setTimeZone() method to change the timezone for the current date.
toString( String format, java.util.TimeZone timeZone ) returns String
Used to format the current date into a string.
format( String format ) returns String
Used to format the current date into a string. Same as toString(format).
toISOString( ) returns String
Returns the date in ISO 8601 format (e.g. "2013-01-04T05:00:00.000Z"). Note that ISO dates are in UTC.
toLong( ) returns long
Returns a long integer used to represent the Date in the following format: "yyyyMMddHHmmssSSS". The time zone is automatically set to UTC. This is useful for perform simple date comparisons and storing dates in a database as integers (e.g. SQLite). Here's an example of how to go from a date to a long and a long to a date:
        javaxt.utils.Date orgDate = new javaxt.utils.Date();
        Long l = orgDate.toLong(); //"yyyyMMddHHmmssSSS" formatted long in UTC
        javaxt.utils.Date newDate = new javaxt.utils.Date(l+"");
        newDate.setTimeZone("UTC", true);
        System.out.println(newDate);
    
Note that this method is different from the getTime() method which returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
clone( ) returns Date
Creates a copy of this object. Any modifications to the clone, will not affect the original.
equals( Object obj ) returns boolean
Used to compare dates and determine whether they are equal.
objAccepts a java.util.Date, a javaxt.utils.Date, or a String.
compareTo( javaxt.utils.Date date, String units ) returns long
Used to compare dates. Returns the number of intervals between two dates. If the given date is in the future, returns a negative value. If the given date is in the past, returns a positive value.
date
unitsUnits of measure (e.g. hours, minutes, seconds, weeks, months, years, etc.)
compareTo( java.util.Date date, String units ) returns long
Used to compare dates. Returns the number of intervals between two dates
date
unitsUnits of measure (e.g. hours, minutes, seconds, weeks, months, years, etc.)
isBefore( String date ) returns boolean
Returns true if a given date is before the current date
isBefore( javaxt.utils.Date Date ) returns boolean
Returns true if a given date is before the current date
isAfter( String date ) returns boolean
Returns true if a given date is after the current date
isAfter( javaxt.utils.Date Date ) returns boolean
Returns true if a given date is after the current date
add( int amount, String units ) returns javaxt.utils.Date
Used to update the current date by adding to (or subtracting from) the current date. Example:
    javaxt.utils.Date date = new javaxt.utils.Date();
    System.out.println("Today is: " + date);
    date.add(-1, "day");
    System.out.println("Yesterday was: " + date);
    
amount
unitsUnit of measure (e.g. hours, minutes, seconds, days, weeks, months, years, etc.)
subtract( int amount, String units ) returns javaxt.utils.Date
Used to update the current date by subtracting from the current date.
amount
unitsUnit of measure (e.g. hours, minutes, seconds, days, weeks, months, years, etc.)
setDate( int year, int month, int day ) returns javaxt.utils.Date
Used to update the year, month and day of the current date.
year
monthValid range is 1-12
dayValid range is 1-31
setTime( int hours, int minutes, int seconds, int milliseconds ) returns javaxt.utils.Date
Used to update the hours, minutes, seconds, and milliseconds of the current date.
getDate( ) returns java.util.Date
Returns the java.utils.Date representation of the current date.
getTime( ) returns long
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
getCalendar( ) returns Calendar
Returns the java.utils.Calender representation of the current date.
getLocalDate( ) returns LocalDate
Returns a java.time.LocalDate representation of the current date.
getWeekdayName( ) returns String
Returns the name of the day of the week. Example: "Monday"
getMonthName( ) returns String
Returns the name of the month. Example: "January"
getDayOfWeek( ) returns int
Returns the day of the week (1-7) where Monday = 1
getWeekInMonth( ) returns int
Returns the week number in a given month. Example: 11/14/2006 = 3
getDayInYear( ) returns int
Returns the day of the year. Example: 11/14/2006 = 318
getWeekInYear( ) returns int
Returns the week number within a given year. Example: 11/14/2006 = 46
getYear( ) returns int
Returns the current year. Example: 11/14/2006 = 2006
getMonth( ) returns int
Returns the current month. Example: 11/14/2006 = 11
getDay( ) returns int
Returns the current day of the month. Example: 11/14/2006 = 14
getHour( ) returns int
Returns the current hour of the day. Example: 12:00 AM = 0, 1:00 PM = 13
getMinute( ) returns int
Returns the current minute of the hour. Example: 12:01 = 1
getSecond( ) returns int
Returns the current second of the minute. Example: 12:00:01 = 1
getMilliSecond( ) returns int
Returns the current millisecond of the second. Example: 12:00:00:01 = 1
hasTimeStamp( ) returns boolean
Returns true if the date has an hour, minute, second, or millisecond greater than zero.
removeTimeStamp( ) returns javaxt.utils.Date
Updates the date by removing the timestamp
compareTo( Object obj ) returns int
Compares two dates for ordering. Older dates appear first in an ordered list like a TreeSet.

Static Methods

getMonthsBetween( javaxt.utils.Date start, javaxt.utils.Date end ) returns double
Returns fractional month difference between two dates. This method will return whole numbers (1, 2, 3, etc) if the two dates fall on the same day of the month (e.g. "2023-03-01" v "2023-04-01" or "2023-03-14" v "2023-04-14"). Returns a decimal value less than or equal to 1 (<=1) if the dates fall within the same month (e.g. "2024-01-01" v "2024-01-31" yields 1.0 and "2024-01-01" v "2024-01-30" yields 0.968). Otherwise, returns the number of full months between the two dates plus a fractional value (e.g. "2023-01-27" v "2023-02-28" yields 1.0357). The decimal value (numbers after the decimal point) represent fractions of a month. Roughly speaking, a day is 0.03 of a month.

There are some interesting results when comparing dates around the end of two different months. Specifically when comparing a longer month to shorter month. For example:

  • "2024-01-31" v "2024-02-29" = 1 month
  • "2023-01-31" v "2023-02-28" = 1 month
  • "2023-12-31" v "2024-02-29" = 2 months
  • "2022-12-31" v "2023-02-29" = 2 months
In these examples we are following semantic rules and are rounding down the differences. However, when we compare dates around the end of two different months if the start month is shorter, we don't round. Example:
  • "2024-04-30" v "2024-05-31" = 1 month, 1 day
  • "2023-02-28" v "2024-02-29" = 12 months, 1 day
In these examples you can see that we are following semantic rules rather than straight math.

Note that you can use the compareTo() method to compare months using the Java standard which rounds down the difference between two months and does not return fractions.

sortDates( java.util.List dates ) returns java.util.List
Static method used to sort dates in a list. Older dates appear first in the output.
getTimeZone( String timezone ) returns java.util.TimeZone
Static method used to return a timezone for a given ID. Unlike the java.util.TimeZone.getTimeZone() method, this method will return a null if a given ID cannot be understood.
timezoneThe name or ID of a TimeZone. Supports common abbreviations such as "EST" or "EDT", full names such as "Eastern Standard Time" or "America/New York", and raw GMT offsets such as "GMT-8:00".
getTimeZones( ) returns HashMap<String, String>
Returns a hashmap of all known time zones. Includes time zones packaged with Java, Microsoft, and a few others.