public final class Day extends java.lang.Object implements java.lang.Comparable<Day>, java.io.Serializable
Note that, internally, this class, like all the other date-related classes in this package, uses the modern Gregorian calendar for all validation and calculations. The Gregorian calendar was introduced in 1582 but this class applies it to earlier dates as well (so using what is known as the "proleptic Gregorian calendar"). This fits with the ISO 8601 standard and is how the java.time package works by default, but it's not how java.util.Calendar works by default, so bear this in mind if you are working with dates from 1582 or before. You are of course unlikely to find good weather data from that far back, but we mention this just in case you find yourself using this class for other purposes as well!
Instances of this class are immutable. You can safely reuse them and call them from multiple threads at once.
| Constructor and Description |
|---|
Day(int year,
int monthOfYearWithJanAs1,
int dayOfMonth)
Constructs a
Day object representing a date with the
specified year, month, and day. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
after(Day comparisonDay)
Returns
true if this Day object is
chronologically after comparisonDay; false
otherwise. |
DayRange |
asRange()
Returns a non-null
DayRange covering this Day
only. |
boolean |
before(Day comparisonDay)
Returns
true if this Day object is
chronologically before comparisonDay; false
otherwise. |
int |
compareTo(Day comparisonDay)
Compares two
Day objects for chronological ordering. |
int |
dayOfMonth()
Returns a number between 1 and 31 (inclusive) indicating the
day-of-the-month component of this
Day. |
DayOfWeek |
dayOfWeek()
Returns a non-null
DayOfWeek object indicating the
day of the week of this Day. |
int |
dayOfYear()
Returns a number between 1 and 366 inclusive (365 for non-leap years)
indicating the day of the year of this
Day. |
int |
daysAfter(Day comparisonDay)
Returns the number of days that this
Day is after
comparisonDay, which will be negative if this
Day comes first chronologically. |
int |
daysBefore(Day comparisonDay)
Returns the number of days that this
Day is before
comparisonDay, which will be negative if
comparisonDay comes first chronologically. |
boolean |
equals(java.lang.Object o)
Returns
true if o is a Day
representing the same date as this Day; false
otherwise. |
static Day |
fromCalendar(java.util.Calendar calendar)
Returns the non-null
Day object represented by the
YEAR, MONTH, and DAY_OF_MONTH
fields of calendar. |
static Day |
fromDateUtc(java.util.Date date)
Returns the non-null
Day object represented by
date, as interpreted in the context of the UTC time-zone. |
static Day |
fromString(java.lang.String dateString)
Parses a string date in ISO
YYYY-MM-DD format (the format
used by XML schema's date type), or YYYYMMDD format. |
DayRange |
fullCalendarMonth()
Returns a non-null
DayRange representing the calendar month
in which this Day falls. |
DayRange |
fullCalendarYear()
Returns a non-null
DayRange representing the calendar year
in which this Day falls. |
DayRange |
fullMonth(StartOfMonth startOfMonth)
Returns a non-null
DayRange representing the month (starting
on the specified StartOfMonth) in which this
Day falls. |
DayRange |
fullWeek(DayOfWeek firstDayOfWeek)
Returns a non-null
DayRange representing the week (starting
on the specified firstDayOfWeek) in which this
Day falls. |
DayRange |
fullYear(StartOfYear startOfYear)
Returns a non-null
DayRange representing the year (starting
on the specified StartOfYear) in which this Day
falls. |
int |
hashCode()
Overridden to ensure consistency with
equals. |
Day |
minusDays(int numberOfDays)
Returns the non-null
Day that comes the specified
numberOfDays before this Day in the calendar. |
Day |
minusMonths(int numberOfMonths)
Returns the non-null
Day that comes the specified
numberOfMonths before this Day in the calendar. |
Day |
minusWeeks(int numberOfWeeks)
Returns the non-null
Day that comes the specified
numberOfWeeks before this Day in the calendar. |
Day |
minusYears(int numberOfYears)
Returns the non-null
Day that comes the specified
numberOfYears before this Day in the calendar. |
int |
monthOfYear()
Returns a number between 1 (January) and 12 (December) indicating the
month-of-the-year component of this
Day. |
Day |
next()
Returns the non-null
Day that comes immediately after this
Day in the calendar. |
static Day |
of(int year,
int monthOfYearWithJanAs1,
int dayOfMonth)
Returns a non-null
Day object representing a date with the
specified year, month, and day. |
Day |
plusDays(int numberOfDays)
Returns the non-null
Day that comes the specified
numberOfDays after this Day in the calendar. |
Day |
plusMonths(int numberOfMonths)
Returns the non-null
Day that comes the specified
numberOfMonths after this Day in the calendar. |
Day |
plusWeeks(int numberOfWeeks)
Returns the non-null
Day that comes the specified
numberOfWeeks after this Day in the calendar. |
Day |
plusYears(int numberOfYears)
Returns the non-null
Day that comes the specified
numberOfYears after this Day in the calendar. |
Day |
previous()
Returns the non-null
Day that comes immediately before this
Day in the calendar. |
DayRange |
to(Day firstOrLastDayInRange)
Returns the non-null
DayRange covering (inclusively) the
days between this Day and firstOrLastDayInRange
(which can be before, after, or equal to this Day). |
DayRange |
to(int toYear,
int toMonthOfYearWithJanAs1,
int toDayOfMonth)
Returns the non-null
DayRange covering (inclusively) the
days between this Day and the Day represented
by the specified parameters (which can be before, after, or equal to this
Day). |
static Day |
todayInSystemTimeZone()
Returns the non-null
Day object representing the current
date in the system time-zone. |
static Day |
todayInUtc()
Returns the non-null
Day object representing the current
date in the UTC time-zone. |
java.lang.String |
toNumericString()
Returns a non-null, non-empty string representation of this
Day in YYYYMMDD format. |
java.lang.String |
toString()
Returns a non-null, non-empty string representation of this
Day in YYYY-MM-DD format. |
int |
year()
Returns a number indicating the year component of this
Day
object. |
public Day(int year,
int monthOfYearWithJanAs1,
int dayOfMonth)
Day object representing a date with the
specified year, month, and day.
It is usually better to call of(int, int, int) as that can re-use cached
instances to conserve memory.year - the year - a number greater than 0 and less than or equal to
9999.monthOfYearWithJanAs1 - the month of the year - a number between 1
and 12 (inclusive), January being 1 and December being 12.dayOfMonth - the day of the month - a number between 1 and 31,
although the upper limit depends on the month (since different
months have different numbers of days), and the year (since
February has 29 days on leap years and 28 otherwise).java.lang.IllegalArgumentException - if any of the parameters are outside of
their allowed ranges i.e. the date doesn't correspond to a real
calendar date, or it is outside the range allowed by
Day (0001-01-01 to 9999-12-31 inclusive).of(int, int, int)public int year()
Day
object.public int monthOfYear()
Day.public int dayOfMonth()
Day.public int dayOfYear()
Day.public static Day of(int year, int monthOfYearWithJanAs1, int dayOfMonth)
Day object representing a date with the
specified year, month, and day.
This will return Day objects from an internal cache when
possible, to conserve memory.year - the year - a number greater than 0 and less than or equal to
9999.monthOfYearWithJanAs1 - the month of the year - a number between 1
and 12 (inclusive), January being 1 and December being 12.dayOfMonth - the day of the month - a number between 1 and 31,
although the upper limit depends on the month (since different
months have different numbers of days), and the year (since
February has 29 days on leap years and 28 otherwise).java.lang.IllegalArgumentException - if any of the parameters are outside of
their allowed ranges i.e. the date doesn't correspond to a real
calendar date, or it is outside the range allowed by
Day (0001-01-01 to 9999-12-31 inclusive).public static Day fromString(java.lang.String dateString) throws java.lang.NumberFormatException
YYYY-MM-DD format (the format
used by XML schema's date type), or YYYYMMDD format. Both
formats can optionally have a '+' in front - XML schema doesn't allow the
'+', but ISO does.Day object corresponding to the date
represented by dateString.java.lang.NullPointerException - if dateString is
null.java.lang.NumberFormatException - if dateString has an invalid
format, or if it represents a non-existent date or one that is
outside the range allowed by Day (0001-01-01 to
9999-12-31 inclusive).toString(),
toNumericString()public static Day fromCalendar(java.util.Calendar calendar)
Day object represented by the
YEAR, MONTH, and DAY_OF_MONTH
fields of calendar. (We correct for the fact that
Calendar.MONTH is zero-based.)java.lang.NullPointerException - if calendar is
null.java.lang.IllegalArgumentException - if the fields of calendar
represent a date that is outside the range allowed by
Day (0001-01-01 to 9999-12-31 inclusive).public static Day fromDateUtc(java.util.Date date)
Day object represented by
date, as interpreted in the context of the UTC time-zone. (A
Date represents a specific instant in time, so the
Day that it corresponds to depends on the time-zone. This
method assumes a UTC time-zone.)java.lang.NullPointerException - if date is null.java.lang.IllegalArgumentException - if date is outside the
range allowed by Day (0001-01-01 to 9999-12-31
inclusive).public static Day todayInSystemTimeZone()
Day object representing the current
date in the system time-zone.java.lang.IllegalArgumentException - if the system clock is configured with a
date that is outside the range allowed by Day
(0001-01-01 to 9999-12-31 inclusive).public static Day todayInUtc()
Day object representing the current
date in the UTC time-zone.java.lang.IllegalArgumentException - if the system clock is configured with a
date that is outside the range allowed by Day
(0001-01-01 to 9999-12-31 inclusive).public boolean equals(java.lang.Object o)
true if o is a Day
representing the same date as this Day; false
otherwise.equals in class java.lang.Objectpublic int hashCode()
equals.hashCode in class java.lang.Objectpublic int compareTo(Day comparisonDay)
Day objects for chronological ordering.compareTo in interface java.lang.Comparable<Day>comparisonDay - the Day to be compared. Cannot be
null.Day is before
comparisonDay; zero if this Day is
equal to comparisonDay; or a positive integer if
this Day is after comparisonDay.java.lang.NullPointerException - if comparisonDay is
null.public boolean before(Day comparisonDay)
true if this Day object is
chronologically before comparisonDay; false
otherwise.comparisonDay - the Day to compare with this
Day. Cannot be null.java.lang.NullPointerException - if comparisonDay is
null.public boolean after(Day comparisonDay)
true if this Day object is
chronologically after comparisonDay; false
otherwise.comparisonDay - the Day to compare with this
Day. Cannot be null.java.lang.NullPointerException - if comparisonDay is
null.public Day next()
Day that comes immediately after this
Day in the calendar. Will happily cross the borders between
months and years.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day previous()
Day that comes immediately before this
Day in the calendar. Will happily cross the borders between
months and years.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day plusDays(int numberOfDays)
Day that comes the specified
numberOfDays after this Day in the calendar.
This will happily cross the borders between months and years, and it will
always ensure that the returned result is a valid Day,
unless it is outside the range allowed by Day (0001-01-01 to
9999-12-31 inclusive), in which case it will throw an
IllegalStateException.
For example 2019-02-14 plus 365 days would give a result of 2020-02-14.
Day is immutable, so calling this method will not change
this Day object.
numberOfDays - the number of days to add to this Day.
Can be positive (to go forwards in time), zero, or negative (to go
backwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day minusDays(int numberOfDays)
Day that comes the specified
numberOfDays before this Day in the calendar.
This will happily cross the borders between months and years, and it will
always ensure that the returned result is a valid Day,
unless it is outside the range allowed by Day (0001-01-01 to
9999-12-31 inclusive), in which case it will throw an
IllegalStateException.
For example 2020-02-14 minus 365 days would give a result of 2019-02-14.
Day is immutable, so calling this method will not change
this Day object.
numberOfDays - the number of days to subtract from this
Day. Can be positive (to go backwards in time), zero,
or negative (to go forwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day plusWeeks(int numberOfWeeks)
Day that comes the specified
numberOfWeeks after this Day in the calendar.
This will happily cross the borders between months and years, and it will
always ensure that the returned result is a valid Day,
unless it is outside the range allowed by Day (0001-01-01 to
9999-12-31 inclusive), in which case it will throw an
IllegalStateException.
For example 2020-07-24 (a Friday) plus 2 weeks would give a result of 2020-08-07 (also a Friday).
Day is immutable, so calling this method will not change
this Day object.
numberOfWeeks - the number of weeks to add to this Day.
Can be positive (to go forwards in time), zero, or negative (to go
backwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day minusWeeks(int numberOfWeeks)
Day that comes the specified
numberOfWeeks before this Day in the calendar.
This will happily cross the borders between months and years, and it will
always ensure that the returned result is a valid Day,
unless it is outside the range allowed by Day (0001-01-01 to
9999-12-31 inclusive), in which case it will throw an
IllegalStateException.
For example 2020-08-07 (a Friday) minus 2 weeks would give a result of 2020-07-24 (also a Friday).
Day is immutable, so calling this method will not change
this Day object.
numberOfWeeks - the number of weeks to subtract from this
Day. Can be positive (to go backwards in time), zero,
or negative (to go forwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day plusMonths(int numberOfMonths)
Day that comes the specified
numberOfMonths after this Day in the calendar.
This will happily cross the borders between years, and it will always
ensure that the returned result is a valid Day, unless it is
outside the range allowed by Day (0001-01-01 to 9999-12-31
inclusive), in which case it will throw an
IllegalStateException.
The dayOfMonth() of the returned Day will be the
same as that of this Day when possible, but sometimes it
will have to be reduced, to fit within the constraints of the resulting
month. For example:
However, the dayOfMonth() will only change when absolutely
necessary, for example:
Day is immutable, so calling this method will not change
this Day object.
numberOfMonths - the number of months to add to this
Day. Can be positive (to go forwards in time), zero,
or negative (to go backwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day minusMonths(int numberOfMonths)
Day that comes the specified
numberOfMonths before this Day in the calendar.
This will happily cross the borders between years, and it will always
ensure that the returned result is a valid Day, unless it is
outside the range allowed by Day (0001-01-01 to 9999-12-31
inclusive), in which case it will throw an
IllegalStateException.
The dayOfMonth() of the returned Day will be the
same as that of this Day when possible, but sometimes it
will have to be reduced, to fit within the constraints of the resulting
month. For example:
However, the dayOfMonth() will only change when absolutely
necessary, for example:
Day is immutable, so calling this method will not change
this Day object.
numberOfMonths - the number of months to subtract from this
Day. Can be positive (to go backwards in time), zero,
or negative (to go forwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day plusYears(int numberOfYears)
Day that comes the specified
numberOfYears after this Day in the calendar.
This will always return a valid Day, unless it is outside
the range allowed by Day (0001-01-01 to 9999-12-31
inclusive), in which case it will throw an
IllegalStateException.
The monthOfYear() of the returned Day will always
be the same as that of this Day. However, the
dayOfMonth() of the returned Day will change if
this Day represents February 29th (in a leap year), and the
resulting Day does not fall in a leap year. In this unusual
situation the dayOfMonth() of the returned Day will
be 28 instead of 29, to ensure that it represents a valid date on the
Gregorian calendar. For example:
Day is immutable, so calling this method will not change
this Day object.
numberOfYears - the number of years to add to this Day.
Can be positive (to go forwards in time), zero, or negative (to go
backwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public Day minusYears(int numberOfYears)
Day that comes the specified
numberOfYears before this Day in the calendar.
This will always return a valid Day, unless it is outside
the range allowed by Day (0001-01-01 to 9999-12-31
inclusive), in which case it will throw an
IllegalStateException.
The monthOfYear() of the returned Day will always
be the same as that of this Day. However, the
dayOfMonth() of the returned Day will change if
this Day represents February 29th (in a leap year), and the
resulting Day does not fall in a leap year. In this unusual
situation the dayOfMonth() of the returned Day will
be 28 instead of 29, to ensure that it represents a valid date on the
Gregorian calendar. For example:
Day is immutable, so calling this method will not change
this Day object.
numberOfYears - the number of years to subtract from this
Day. Can be positive (to go backwards in time), zero,
or negative (to go forwards in time).Day object representing the result of the
operation.java.lang.IllegalStateException - in the unlikely event of this method
requiring a Day object with a year less than 1 or
greater than 9999.public DayRange to(Day firstOrLastDayInRange)
DayRange covering (inclusively) the
days between this Day and firstOrLastDayInRange
(which can be before, after, or equal to this Day).firstOrLastDayInRange - the Day that the returned
DayRange should extend from this Day to.
Can be before, after, or equal to this Day. Cannot be
null.DayRange object that extends from the
earlier of this Day and
firstOrLastDayInRange to the later of those two
Day objects.java.lang.NullPointerException - if firstOrLastDayInRange is
null.to(int, int, int)public DayRange to(int toYear, int toMonthOfYearWithJanAs1, int toDayOfMonth)
DayRange covering (inclusively) the
days between this Day and the Day represented
by the specified parameters (which can be before, after, or equal to this
Day).toYear - the year of the Day that this Day
should be extended to - a number greater than 0 and less than or
equal to 9999.toMonthOfYearWithJanAs1 - the month-of-the-year of the
Day that this Day should be extended to
- a number between 1 and 12 (inclusive), January being 1 and
December being 12.toDayOfMonth - the day-of-the-month of the Day that
this Day should be extended to - a number between 1
and 31, although the upper limit depends on the month (since
different months have different numbers of days), and the year
(since February has 29 days on leap years and 28 otherwise).java.lang.IllegalArgumentException - if any of the parameters are outside of
their allowed ranges i.e. the to-date specified doesn't
correspond to a real calendar date, or it is outside the range
allowed by Day (0001-01-01 to 9999-12-31 inclusive).to(Day)public DayRange asRange()
DayRange covering this Day
only.
This is essentially a convenience method. Instead of typing
Day.of(2011, 1, 1).to(2011, 1, 1)
you can type
Day.of(2011, 1, 1).asRange()
public int daysAfter(Day comparisonDay)
Day is after
comparisonDay, which will be negative if this
Day comes first chronologically.
For example:
Day.of(2020, 5, 15).daysAfter(Day.of(2020, 5, 14)) will
return 1.Day.of(2020, 7, 18).daysAfter(Day.of(2020, 7, 20)) will
return -2.Day.of(1999, 12, 31).daysAfter(Day.of(1999, 12, 31))
will return 0.java.lang.NullPointerException - if comparisonDay is
null.daysBefore(net.degreedays.time.Day)public int daysBefore(Day comparisonDay)
Day is before
comparisonDay, which will be negative if
comparisonDay comes first chronologically.
For example:
Day.of(2020, 7, 18).daysBefore(Day.of(2020, 7, 20)) will
return 2.Day.of(2020, 5, 15).daysBefore(Day.of(2020, 5, 14)) will
return -1.Day.of(1999, 12, 31).daysBefore(Day.of(1999, 12, 31))
will return 0.java.lang.NullPointerException - if comparisonDay is
null.daysAfter(net.degreedays.time.Day)public DayOfWeek dayOfWeek()
DayOfWeek object indicating the
day of the week of this Day.public DayRange fullMonth(StartOfMonth startOfMonth)
DayRange representing the month (starting
on the specified StartOfMonth) in which this
Day falls.
For example
Day.of(2020, 6, 21).fullMonth(StartOfMonth.of(15)) would
return a DayRange covering 2020-06-15 to 2020-07-14
(inclusive).
startOfMonth - indicating the first day of a "month" e.g.
StartOfMonth.of(1) for calendar months starting on
the 1st. Cannot be null.java.lang.NullPointerException - if startOfMonth is
null.java.lang.IllegalStateException - in the unlikely event of this method
requiring a DayRange extending outside of the range
allowed by Day (0001-01-01 to 9999-12-31 inclusive).fullCalendarMonth()public DayRange fullCalendarMonth()
DayRange representing the calendar month
in which this Day falls.
For example Day.of(2020, 6, 21).fullCalendarMonth() would
return a DayRange covering 2020-06-01 to 2020-06-30
(inclusive).
java.lang.IllegalStateException - in the unlikely event of this method
requiring a DayRange extending outside of the range
allowed by Day (0001-01-01 to 9999-12-31 inclusive).fullMonth(net.degreedays.time.StartOfMonth)public DayRange fullYear(StartOfYear startOfYear)
DayRange representing the year (starting
on the specified StartOfYear) in which this Day
falls.
For example
Day.of(2020, 6, 21).fullYear(StartOfYear.of(4, 6)) would
return a DayRange covering 2020-04-06 to 2021-04-05
(inclusive).
startOfYear - indicating the first day of a "year" e.g.
StartOfYear.of(1, 1) for calendar years starting on
January 1st. Cannot be null.java.lang.NullPointerException - if startOfYear is
null.java.lang.IllegalStateException - in the unlikely event of this method
requiring a DayRange extending outside of the range
allowed by Day (0001-01-01 to 9999-12-31 inclusive).fullCalendarYear()public DayRange fullCalendarYear()
DayRange representing the calendar year
in which this Day falls.
For example Day.of(2020, 6, 21).fullCalendarYear() would
return a DayRange covering 2020-01-01 to 2020-12-31
(inclusive).
java.lang.IllegalStateException - in the unlikely event of this method
requiring a DayRange extending outside of the range
allowed by Day (0001-01-01 to 9999-12-31 inclusive).fullYear(net.degreedays.time.StartOfYear)public DayRange fullWeek(DayOfWeek firstDayOfWeek)
DayRange representing the week (starting
on the specified firstDayOfWeek) in which this
Day falls.
For example Day.of(2020, 6, 21).fullWeek(DayOfWeek.MONDAY)
would return a DayRange covering Monday 2020-06-15 to Sunday
2020-06-21 (inclusive).
firstDayOfWeek - indicating the first day of a "week" e.g.
DayOfWeek.MONDAY for weeks running from Monday to
Sunday (inclusive). Cannot be null.java.lang.NullPointerException - if firstDayOfWeek is
null.java.lang.IllegalStateException - in the unlikely event of this method
requiring a DayRange extending outside of the range
allowed by Day (0001-01-01 to 9999-12-31 inclusive).public java.lang.String toNumericString()
Day in YYYYMMDD format.toString(),
fromString(java.lang.String)public java.lang.String toString()
Day in YYYY-MM-DD format.toString in class java.lang.ObjecttoNumericString(),
fromString(java.lang.String)
See www.degreedays.net/api/ for more about the Degree Days.net API.
You can sign up for a Degree Days.net API account and read the integration guide that is useful and applicable whether you are using Java or not.