Package com.norconex.commons.lang.time
Class YearMonthDay
java.lang.Object
com.norconex.commons.lang.time.YearMonthDay
- All Implemented Interfaces:
Serializable,Comparable<YearMonthDay>
Immutable class holding a specific date made only of the
year, month, and day. The months are starting
at 1 (January). Similar to
LocalDate, but build around Date.
Operations involving time (e.g. toDate()) use UTC.- Since:
- 1.3.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a YearMonthDay with the current date (UTC).YearMonthDay(int year) Constructs a YearMonthDay with the specified year.YearMonthDay(int year, int month) Constructs a YearMonthDay with the specified year and month.YearMonthDay(int year, int month, int day) Constructs a YearMonthDay with the specified values.YearMonthDay(@NonNull String date) Constructs a YearMonthDay from a string of this format:yyyy-MM-dd.YearMonthDay(@NonNull LocalDate localDate) Constructs a YearMonthDay from aLocalDate.YearMonthDay(@NonNull Calendar calendar) Constructs a YearMonthDay from aCalendar.YearMonthDay(@NonNull Date date) Constructs a YearMonthDay from aDate(UTC). -
Method Summary
Modifier and TypeMethodDescriptionaddDays(int numOfDays) Adds a number of days to a new created YearMonthDay.addMonths(int numOfMonths) Adds a number of months to a new created YearMonthDay.addYears(int numOfYears) Adds a number of years to a new created YearMonthDay.intcompareTo(YearMonthDay ymd) booleanWhether this YearmMonthDay contains the givenDate(i.e. same year, month, and day, regardless of time).booleanintgetDay()Gets the day.intgetMonth()Gets the month.intgetYear()Gets the year.inthashCode()booleanisAfter(YearMonthDay ymd) Whether this YearMonthDay represents a date after the given YearMonthDay.booleanisAfterDate(Date date) Whether this YearMonthDay represents a date after the given date.booleanisBefore(YearMonthDay ymd) Whether this YearMonthDay represents a date before the given YearMonthDay.booleanisBeforeDate(Date date) Whether this YearMonthDay represents a date before the given date.Converts this YearMonthDay to aCalendarat midnight.toDate()Converts this YearMonthDay to aDateat midnight (00:00) UTC.Gets the date as midnight the day after to represent the end of the day.Gets this YearMonthDay as aLocalDate.Gets this YearMonthDay as aLocalDateTimeat midnight (00:00).longtoMillis()Converts this YearMonthDay to the current time as UTC milliseconds from the epoch.toString()Converts the YearMonthDay to a string of this format:yyyy-MM-dd.Formats this YeamMonthDay using pattern build fromSimpleDateFormatsyntax, after first converting it toDate.
-
Constructor Details
-
YearMonthDay
public YearMonthDay()Constructs a YearMonthDay with the current date (UTC). -
YearMonthDay
public YearMonthDay(int year, int month, int day) Constructs a YearMonthDay with the specified values.- Parameters:
year- the yearmonth- the monthday- the day
-
YearMonthDay
public YearMonthDay(int year, int month) Constructs a YearMonthDay with the specified year and month. The day is set to 1.- Parameters:
year- the yearmonth- the month
-
YearMonthDay
public YearMonthDay(int year) Constructs a YearMonthDay with the specified year. The day and the months are both set to 1.- Parameters:
year- the year
-
YearMonthDay
Constructs a YearMonthDay from aDate(UTC).- Parameters:
date- a date
-
YearMonthDay
Constructs a YearMonthDay from aCalendar.- Parameters:
calendar- a calendar instant
-
YearMonthDay
Constructs a YearMonthDay from a string of this format:yyyy-MM-dd.- Parameters:
date- a date string
-
YearMonthDay
Constructs a YearMonthDay from aLocalDate.- Parameters:
localDate- a local date- Since:
- 3.0.0
-
-
Method Details
-
getYear
public int getYear()Gets the year.- Returns:
- the year
-
getMonth
public int getMonth()Gets the month.- Returns:
- the month
-
getDay
public int getDay()Gets the day.- Returns:
- the day
-
isBefore
Whether this YearMonthDay represents a date before the given YearMonthDay.- Parameters:
ymd- the YearMonthDay to evaluate- Returns:
trueif before
-
isBeforeDate
Whether this YearMonthDay represents a date before the given date.- Parameters:
date- the date to evaluate- Returns:
trueif before
-
isAfter
Whether this YearMonthDay represents a date after the given YearMonthDay.- Parameters:
ymd- the YearMonthDay to evaluate- Returns:
trueif after
-
isAfterDate
Whether this YearMonthDay represents a date after the given date.- Parameters:
date- the date to evaluate- Returns:
trueif after
-
contains
Whether this YearmMonthDay contains the givenDate(i.e. same year, month, and day, regardless of time).- Parameters:
date- date to evaluate- Returns:
trueif date is contained
-
toDate
Converts this YearMonthDay to aDateat midnight (00:00) UTC.- Returns:
- a date
-
toMillis
public long toMillis()Converts this YearMonthDay to the current time as UTC milliseconds from the epoch.- Returns:
- milliseconds
-
toEndOfDayDate
Gets the date as midnight the day after to represent the end of the day. This ensures all dates on the same day as this YearMonthDay are smaller than this returned exclusive date. Useful for date range comparisons where typically the end date is exclusive.- Returns:
- midnight past the end date
-
addDays
Adds a number of days to a new created YearMonthDay. For subtractions, user a negative integer.- Parameters:
numOfDays- number of days- Returns:
- a new YearMonthDay instance
-
addMonths
Adds a number of months to a new created YearMonthDay. For subtractions, user a negative integer.- Parameters:
numOfMonths- number of months- Returns:
- a new YearMonthDay instance
-
addYears
Adds a number of years to a new created YearMonthDay. For subtractions, user a negative integer.- Parameters:
numOfYears- number of years- Returns:
- a new YearMonthDay instance
-
toCalendar
Converts this YearMonthDay to aCalendarat midnight.- Returns:
- a calendar
-
toLocalDate
Gets this YearMonthDay as aLocalDate.- Returns:
- the local date
- Since:
- 3.0.0
-
toLocalDateTime
Gets this YearMonthDay as aLocalDateTimeat midnight (00:00).- Returns:
- the local date time
- Since:
- 3.0.0
-
compareTo
- Specified by:
compareToin interfaceComparable<YearMonthDay>
-
toString
Converts the YearMonthDay to a string of this format:yyyy-MM-dd. -
toString
Formats this YeamMonthDay using pattern build fromSimpleDateFormatsyntax, after first converting it toDate.- Parameters:
pattern- the pattern used for formatting- Returns:
- the formatted string
-
equals
-
hashCode
public int hashCode()
-