Class LocalMonth
- All Implemented Interfaces:
Serializable
,Comparable<LocalMonth>
- Author:
- ralph
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns the end of this month as aLocalDateTime
.atEndOfMonth
(ZoneId zone) Returns the end of this month as aZonedDateTime
using a specific time zone.Returns the start of this month as aLocalDateTime
.atStartOfMonth
(ZoneId zone) Returns the start of this month as aZonedDateTime
using a specific time zone.int
compareTo
(LocalMonth otherMonth) boolean
static LocalMonth
from
(TemporalAccessor temporal) Obtains an instance ofLocalMonth
from a temporal object.Returns the first day of this month as aLocalDate
.getKey()
Returns the identifiable key for this month in format "YYYYMM".Returns the last day of this month as aLocalDate
.getMonth()
Gets the month-of-year field using theMonth
enum.int
Gets the month-of-year field from 1 to 12.int
getYear()
Gets the year field.int
hashCode()
boolean
Checks if the year is a leap year, according to the ISO proleptic calendar system rules.int
Returns the length of the month.int
Returns the length of the year represented by this month.minusMonths
(int monthsToSubtract) Returns a copy of thisLocalMonth
with the specified number of months subtracted.minusYear
(int yearsToSubtract) Returns a copy of thisLocalMonth
with the specified number of years subtracted.monthsUntil
(LocalMonth endExclusive) Returns a sequential ordered stream of months.Returns the subsequent (following) month.static LocalMonth
now()
Obtains the current month from the system clock in the default time-zone.static LocalMonth
Obtains the current month from the specified clock.static LocalMonth
Obtains the current month from the system clock in the specified time-zone.static LocalMonth
of
(int year, int month) Obtains an instance ofLocalMonth
from a year and month.static LocalMonth
Obtains an instance ofLocalMonth
from a year and month.static LocalMonth
ofEpochDay
(int epochDay) Obtains an instance ofLocalMonth
from the epoch day count.static LocalMonth
ofEpochMonth
(int epochMonth) Obtains an instance ofLocalMonth
from the epoch month count.static LocalMonth
Obtains an instance ofLocalMOnth
from anInstant
and zone ID.static LocalMonth
Obtains an instance ofLocalMonth
from its key string.static LocalMonth
ofLocalDate
(LocalDate localDate) Obtains an instance ofLocalMonth
from the local date.static LocalMonth
ofYearDay
(int year, int dayOfYear) Obtains an instance ofLocalMonth
from a year and day-of-year.static LocalMonth
parse
(CharSequence text) Obtains an instance ofLocalMonth
from a text string such as2007-12-03
.static LocalMonth
parse
(CharSequence text, DateTimeFormatter formatter) Obtains an instance ofLocalMonth
from a text string using a specific formatter.plusMonths
(int monthsToAdd) Returns a copy of thisLocalMonth
with the specified number of months added.plusYear
(int yearsToAdd) Returns a copy of thisLocalMonth
with the specified number of years added.Returns the previous monthint
Obtains epoch month count.toString()
withMonth
(int month) Returns a copy of thisLocalMonth
with the month-of-year altered.withYear
(int year) Returns a copy of thisLocalMonth
with the year altered.
-
Method Details
-
parse
Obtains an instance ofLocalMonth
from a text string such as2007-12-03
.The string must represent a valid date and is parsed using
DateTimeFormatter.ISO_LOCAL_DATE
.- Parameters:
text
- the text to parse such as "2007-12-03", not null- Returns:
- the parsed local month, not null
- Throws:
DateTimeParseException
- if the text cannot be parsed
-
parse
Obtains an instance ofLocalMonth
from a text string using a specific formatter.The text is parsed using the formatter, returning a date.
- Parameters:
text
- the text to parse, not nullformatter
- the formatter to use, not null- Returns:
- the parsed local month, not null
- Throws:
DateTimeParseException
- if the text cannot be parsed
-
from
Obtains an instance ofLocalMonth
from a temporal object.This obtains a local month based on the specified temporal. A
TemporalAccessor
represents an arbitrary set of date and time information, which this factory converts to an instance ofLocalMonth
.The conversion uses the
TemporalQueries.localDate()
query, which relies on extracting theEPOCH_DAY
field.- Parameters:
temporal
- the temporal object to convert, not null- Returns:
- the local month, not null
- Throws:
DateTimeException
- if unable to convert to aLocalMonth
-
now
Obtains the current month from the system clock in the default time-zone.This will query the
system clock
in the default time-zone to obtain the current date.Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
- Returns:
- the current month using the system clock and default time-zone, not null
-
now
Obtains the current month from the specified clock.This will query the specified clock to obtain the current date - today. Using this method allows the use of an alternate clock for testing. The alternate clock may be introduced using
dependency injection
.- Parameters:
clock
- the clock to use, not null- Returns:
- the current month, not null
-
now
Obtains the current month from the system clock in the specified time-zone.This will query the
system clock
to obtain the current date. Specifying the time-zone avoids dependence on the default time-zone.Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
- Parameters:
zoneId
- the zone ID to use, not null- Returns:
- the current month using the system clock, not null
-
of
Obtains an instance ofLocalMonth
from a year and month.This returns a
LocalMonth
with the specified year and month.- Parameters:
year
- the year to represent, from MIN_YEAR to MAX_YEARmonth
- the month-of-year to represent, from 1 (January) to 12 (December)- Returns:
- the local month, not null
-
of
Obtains an instance ofLocalMonth
from a year and month.This returns a
LocalMonth
with the specified year and month.- Parameters:
year
- the year to represent, from MIN_YEAR to MAX_YEARmonth
- the month-of-year to represent, not null- Returns:
- the local month, not null
-
ofInstant
Obtains an instance ofLocalMOnth
from anInstant
and zone ID.This creates a local month based on the specified instant. First, the offset from UTC/Greenwich is obtained using the zone ID and instant, which is simple as there is only one valid offset for each instant. Then, the instant and offset are used to calculate the local date.
- Parameters:
instant
- the instant to create the date from, not nullzone
- the time-zone, which may be an offset, not null- Returns:
- the local month, not null
- Throws:
DateTimeException
- if the result exceeds the supported range- Since:
- 9
-
ofEpochDay
Obtains an instance ofLocalMonth
from the epoch day count.This returns a
LocalMonth
based on the specified epoch-day. TheEPOCH_DAY
is a simple incrementing count of days where day 0 is 1970-01-01. Negative numbers represent earlier days.- Parameters:
epochDay
- the Epoch Day to convert, based on the epoch 1970-01-01- Returns:
- the local month, not null
- Throws:
DateTimeException
- if the epoch day exceeds the supported date range
-
ofEpochMonth
Obtains an instance ofLocalMonth
from the epoch month count.This returns a
LocalMonth
based on the specified epoch-month. The emoch-month is a simple incrementing count of months where month 0 is January 1970. Negative numbers represent earlier months.- Parameters:
epochMonth
- the Epoch Month to convert, based on the epoch 1970-01- Returns:
- the local month, not null
-
ofLocalDate
Obtains an instance ofLocalMonth
from the local date.This returns a
LocalMonth
based on the specified local date.- Parameters:
localDate
- the local date object- Returns:
- the local month, not null
-
ofYearDay
Obtains an instance ofLocalMonth
from a year and day-of-year.This returns a
LocalMonth
with the specified year and day-of-year. The day-of-year must be valid for the year, otherwise an exception will be thrown.- Parameters:
year
- the year to represent, from MIN_YEAR to MAX_YEARdayOfYear
- the day-of-year to represent, from 1 to 366- Returns:
- the local month, not null
- Throws:
DateTimeException
- if the value of any field is out of range, or if the day-of-year is invalid for the year
-
ofKey
Obtains an instance ofLocalMonth
from its key string.This returns a
LocalMonth
with the specified year and month in key.- Parameters:
key
- the key of the month, e.g. "202209".- Returns:
- the local month, not null
- Throws:
NumberFormatException
- - if the string does not contain parsable integers.
-
getYear
public int getYear()Gets the year field.This method returns the primitive
int
value for the year.The year returned by this method is proleptic as per
get(YEAR)
. To obtain the year-of-era, useget(YEAR_OF_ERA)
.- Returns:
- the year, from MIN_YEAR to MAX_YEAR
-
getMonthValue
public int getMonthValue()Gets the month-of-year field from 1 to 12.This method returns the month as an
int
from 1 to 12. Application code is frequently clearer if the enumMonth
is used by callinggetMonth()
.- Returns:
- the month-of-year, from 1 to 12
- See Also:
-
getMonth
Gets the month-of-year field using theMonth
enum.This method returns the enum
Month
for the month. This avoids confusion as to whatint
values mean. If you need access to the primitiveint
value then the enum provides theint value
.- Returns:
- the month-of-year, not null
- See Also:
-
isLeapYear
public boolean isLeapYear()Checks if the year is a leap year, according to the ISO proleptic calendar system rules.This method applies the current rules for leap years across the whole time-line. In general, a year is a leap year if it is divisible by four without remainder. However, years divisible by 100, are not leap years, with the exception of years divisible by 400 which are.
For example, 1904 is a leap year it is divisible by 4. 1900 was not a leap year as it is divisible by 100, however 2000 was a leap year as it is divisible by 400.
The calculation is proleptic - applying the same rules into the far future and far past. This is historically inaccurate, but is correct for the ISO-8601 standard.
- Returns:
- true if the year is leap, false otherwise
-
lengthOfMonth
public int lengthOfMonth()Returns the length of the month.This returns the length of the month in days. For example, a date in January would return 31.
- Returns:
- the length of the month in days
-
lengthOfYear
public int lengthOfYear()Returns the length of the year represented by this month.This returns the length of the year in days, either 365 or 366.
- Returns:
- 366 if the year is leap, 365 otherwise
-
getKey
Returns the identifiable key for this month in format "YYYYMM".- Returns:
- the key of this month
-
getFirstDay
Returns the first day of this month as aLocalDate
.- Returns:
- the first day in this month
-
getLastDay
Returns the last day of this month as aLocalDate
.- Returns:
- the last day in this month
-
atStartOfMonth
Returns the start of this month as aLocalDateTime
.- Returns:
- the start of this month
-
atStartOfMonth
Returns the start of this month as aZonedDateTime
using a specific time zone.- Parameters:
zone
- the time zone for the return value- Returns:
- the start of this month
-
atEndOfMonth
Returns the end of this month as aLocalDateTime
.- Returns:
- the end of this month
-
atEndOfMonth
Returns the end of this month as aZonedDateTime
using a specific time zone.- Parameters:
zone
- the time zone for the return value- Returns:
- the end of this month
-
toEpochMonth
public int toEpochMonth()Obtains epoch month count.This returns epoch-month represented by this LocalMonth. The emoch-month is a simple incrementing count of months where month 0 is January 1970. Negative numbers represent earlier months.
- Returns:
- the Epoch Month, based on the epoch 1970-01
-
monthsUntil
Returns a sequential ordered stream of months. The returned stream starts from this month (inclusive) and goes toendExclusive
(exclusive) by an incremental step of 1 month.- Parameters:
endExclusive
- the end month, exclusive, not null- Returns:
- a sequential
Stream
for the range ofLocalMonth
values - Throws:
IllegalArgumentException
- if end month is before this month
-
plusMonths
Returns a copy of thisLocalMonth
with the specified number of months added.This instance is immutable and unaffected by this method call.
- Parameters:
monthsToAdd
- the months to add, may be negative- Returns:
- a
LocalMonth
based on this date with the months added, not null
-
plusYear
Returns a copy of thisLocalMonth
with the specified number of years added.This instance is immutable and unaffected by this method call.
- Parameters:
yearsToAdd
- the years to add, may be negative- Returns:
- a
LocalMonth
based on this date with the years added, not null
-
minusMonths
Returns a copy of thisLocalMonth
with the specified number of months subtracted.This instance is immutable and unaffected by this method call.
- Parameters:
monthsToSubtract
- the months to subtract, may be negative- Returns:
- a
LocalMonth
based on this date with the months subtracted, not null
-
minusYear
Returns a copy of thisLocalMonth
with the specified number of years subtracted.This instance is immutable and unaffected by this method call.
- Parameters:
yearsToSubtract
- the years to subtract, may be negative- Returns:
- a
LocalMonth
based on this date with the years subtracted, not null
-
nextMonth
Returns the subsequent (following) month.- Returns:
- the next month
-
previousMonth
Returns the previous month- Returns:
- the month before.
-
withMonth
Returns a copy of thisLocalMonth
with the month-of-year altered.This instance is immutable and unaffected by this method call.
- Parameters:
month
- the month-of-year to set in the result, from 1 (January) to 12 (December)- Returns:
- a
LocalMOnth
based on this date with the requested month, not null - Throws:
DateTimeException
- if the month-of-year value is invalid
-
withYear
Returns a copy of thisLocalMonth
with the year altered.This instance is immutable and unaffected by this method call.
- Parameters:
year
- the year to set in the result, from MIN_YEAR to MAX_YEAR- Returns:
- a
LocalMonth
based on this date with the requested year, not null - Throws:
DateTimeException
- if the year value is invalid
-
compareTo
- Specified by:
compareTo
in interfaceComparable<LocalMonth>
-
hashCode
public int hashCode() -
equals
-
toString
-