org.enblom.time
Interface DayDate

All Superinterfaces:
Comparable<DayDate>, Serializable

public interface DayDate
extends Comparable<DayDate>, Serializable

Represents a date - year, month, day in the range Jan 1, 1000 - Dec 31, 9999, with a lot of utilities.

The framework is aware of leap years, and checks all dates for validity.

Date objects are always immutable - they cannot be changed once created.

The natural order of this class orders instances in chronological order, and two instances are considered equal if they represent the same year, month and day.

Example usage

Create new instances: DayDate.factory.now()
DayDate.factory.getDefault()
DayDate.factory.parse(...)
Formatting: DayDate.iso().formatXXX()
DayDate.eur().formatXXX()
DayDate.us().formatXXX()
Serializing DayDate.serialize()
DayDate.factory.deserialize()

Author:
Andreas Enblom

Field Summary
static DayDateFactory factory
          A default implementaton of DayDateFactory, for creating new instances.
 
Method Summary
 int day()
           
 boolean equals(Object other)
           
 DayDateFormatter eur()
          Provides a formatter for this date instance that formats day dates according to some de-facto European standard.
 DayOfWeek getDayOfWeek()
          Calculates the day of week of this date.
 int hashCode()
           
 boolean isLaterMonthThan(DayDate date)
          Determines whether this is in a later month than the given date.
 boolean isLaterYearThan(DayDate date)
          Determines whether this is in a later year than the given date.
 DayDateFormatter iso()
          Provides a formatter for this date instance that formats day dates according to the ISO-8601 standard.
 boolean isSameMonthAs(DayDate date)
          Determines whether this is in the same month and the same year as the given date.
 boolean isSameYearAs(DayDate date)
          Determines whether this is in the same year as the given date.
 Month month()
           
 DayDate plusDays(int offset)
          Adds or subtracts the given number of days to this date.
 DayDate plusMonths(int offset)
          Adds or subtracts the given number of months to this date.
 DayDate plusYears(int offset)
          Adds or subtracts the given number of years to this date.
 String serialize()
           
 int toInt()
          Creates an int representation of the date, which when written in base 10 is the string YYYYMMDD.
 String toString()
          Overrides Object.toString().
 DayDateFormatter us()
          Provides a formatter for this date instance that formats day dates according to the US standard.
 int year()
           
 
Methods inherited from interface java.lang.Comparable
compareTo
 

Field Detail

factory

static final DayDateFactory factory
A default implementaton of DayDateFactory, for creating new instances. The factory will create serializable instances.

Method Detail

year

int year()
Returns:
The year represented by this date in the range 1000-9999.

month

Month month()
Returns:
The month represented by this date.

day

int day()
Returns:
The day of month represented by this date in the range 1-28, 1-29, 1-30 or 1-31, depending on what month and year it is. Leap-years are handled correctly.

getDayOfWeek

DayOfWeek getDayOfWeek()
Calculates the day of week of this date.

Returns:
The day of week.

plusYears

DayDate plusYears(int offset)
Adds or subtracts the given number of years to this date. If the resulting date is out of range, a runtime exception is thrown.

In the rare case that this is Feb 29 on a leap year, and adding the given offset results in a non leap year, the date will be set to Feb 28.

Parameters:
offset - The offset in years. Can be positive, zero or negative.
Returns:
The resulting date.
Throws:
IllegalArgumentException - If the resulting date is out of range.

plusMonths

DayDate plusMonths(int offset)
Adds or subtracts the given number of months to this date. If the resulting date is out of range, a runtime exception is thrown.

If adding the given number of months would make the day of month over the new month's maximum it will be reduced to the proper maximum. Hence, adding three months to 2011-05-31 will give 2011-08-31, but adding four will give 2011-09-30. And adding 13 months to 2010-01-30 will give the date 2011-02-28.

Parameters:
offset - The offset in months. Can be positive, zero or negative.
Returns:
The resulting date.
Throws:
IllegalArgumentException - If the resulting date is out of range.

plusDays

DayDate plusDays(int offset)
Adds or subtracts the given number of days to this date. If the resulting date is out of range, a runtime exception is thrown.

Parameters:
offset - The offset in days. Can be positive, zero or negative.
Returns:
The resulting time.
Throws:
IllegalArgumentException - If the resulting date is out of range.

isLaterYearThan

boolean isLaterYearThan(DayDate date)
Determines whether this is in a later year than the given date.

Parameters:
date - The date to compare to.
Returns:
Whether this is in a later year than the given date.

isSameYearAs

boolean isSameYearAs(DayDate date)
Determines whether this is in the same year as the given date.

Parameters:
date - The date to compare to.
Returns:
Whether this is in the same year as the given date.

isLaterMonthThan

boolean isLaterMonthThan(DayDate date)
Determines whether this is in a later month than the given date. A month of a later year is always considered to be later, e.g. Jan, 2011 is a later month than Nov, 2010.

Parameters:
date - The date to compare to.
Returns:
Whether this is in a later month than the given date.

isSameMonthAs

boolean isSameMonthAs(DayDate date)
Determines whether this is in the same month and the same year as the given date.

Parameters:
date - The date to compare to.
Returns:
Whether this is in the same month as the given date.

hashCode

int hashCode()
Overrides:
hashCode in class Object

equals

boolean equals(Object other)
Overrides:
equals in class Object

iso

DayDateFormatter iso()
Provides a formatter for this date instance that formats day dates according to the ISO-8601 standard. This means that dates are formatted as YYYY-MM-DD and YYYYMMDD.

See also eur() and us().

Returns:
A formatter for this date that uses the ISO-8601 standard.

eur

DayDateFormatter eur()
Provides a formatter for this date instance that formats day dates according to some de-facto European standard. This means that dates are formatted as DD.MM.YYYY and DDMMYYYY.

See also iso() and us().

Returns:
A formatter for this date that uses some de-facto European standard.

us

DayDateFormatter us()
Provides a formatter for this date instance that formats day dates according to the US standard. This means that dates are formatted as MM/DD/YYYY and MMDDYYYY.

See also iso() and eur().

Returns:
A formatter for this date that uses the US standard.

serialize

String serialize()
Returns:
The date on the format YYYYMMDD.

toString

String toString()
Overrides Object.toString().

Overrides:
toString in class Object
Returns:
The date on the format YYYY-MM-DD

toInt

int toInt()
Creates an int representation of the date, which when written in base 10 is the string YYYYMMDD. E.g, the date 2011-03-02 is represented as the integer 20110302.

Returns:
The int representation.


Copyright © 2013. All Rights Reserved.