Class LocalMonth

java.lang.Object
rs.baselib.time.LocalMonth
All Implemented Interfaces:
Serializable, Comparable<LocalMonth>

public class LocalMonth extends Object implements Serializable, Comparable<LocalMonth>
Replacement for RsMonth.
Author:
ralph
See Also:
  • Method Details

    • parse

      public static LocalMonth parse(CharSequence text)
      Obtains an instance of LocalMonth from a text string such as 2007-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

      public static LocalMonth parse(CharSequence text, DateTimeFormatter formatter)
      Obtains an instance of LocalMonth 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 null
      formatter - the formatter to use, not null
      Returns:
      the parsed local month, not null
      Throws:
      DateTimeParseException - if the text cannot be parsed
    • from

      public static LocalMonth from(TemporalAccessor temporal)
      Obtains an instance of LocalMonth 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 of LocalMonth.

      The conversion uses the TemporalQueries.localDate() query, which relies on extracting the EPOCH_DAY field.

      Parameters:
      temporal - the temporal object to convert, not null
      Returns:
      the local month, not null
      Throws:
      DateTimeException - if unable to convert to a LocalMonth
    • now

      public static LocalMonth 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

      public static LocalMonth now(Clock clock)
      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

      public static LocalMonth now(ZoneId zoneId)
      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

      public static LocalMonth of(int year, int month)
      Obtains an instance of LocalMonth 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_YEAR
      month - the month-of-year to represent, from 1 (January) to 12 (December)
      Returns:
      the local month, not null
    • of

      public static LocalMonth of(int year, Month month)
      Obtains an instance of LocalMonth 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_YEAR
      month - the month-of-year to represent, not null
      Returns:
      the local month, not null
    • ofInstant

      public static LocalMonth ofInstant(Instant instant, ZoneId zoneId)
      Obtains an instance of LocalMOnth from an Instant 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 null
      zone - 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

      public static LocalMonth ofEpochDay(int epochDay)
      Obtains an instance of LocalMonth from the epoch day count.

      This returns a LocalMonth based on the specified epoch-day. The EPOCH_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

      public static LocalMonth ofEpochMonth(int epochMonth)
      Obtains an instance of LocalMonth 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

      public static LocalMonth ofLocalDate(LocalDate localDate)
      Obtains an instance of LocalMonth 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

      public static LocalMonth ofYearDay(int year, int dayOfYear)
      Obtains an instance of LocalMonth 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_YEAR
      dayOfYear - 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

      public static LocalMonth ofKey(String key)
      Obtains an instance of LocalMonth 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, use get(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 enum Month is used by calling getMonth().

      Returns:
      the month-of-year, from 1 to 12
      See Also:
    • getMonth

      public Month getMonth()
      Gets the month-of-year field using the Month enum.

      This method returns the enum Month for the month. This avoids confusion as to what int values mean. If you need access to the primitive int value then the enum provides the int 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

      public String getKey()
      Returns the identifiable key for this month in format "YYYYMM".
      Returns:
      the key of this month
    • getFirstDay

      public LocalDate getFirstDay()
      Returns the first day of this month as a LocalDate.
      Returns:
      the first day in this month
    • getLastDay

      public LocalDate getLastDay()
      Returns the last day of this month as a LocalDate.
      Returns:
      the last day in this month
    • atStartOfMonth

      public LocalDateTime atStartOfMonth()
      Returns the start of this month as a LocalDateTime.
      Returns:
      the start of this month
    • atStartOfMonth

      public ZonedDateTime atStartOfMonth(ZoneId zone)
      Returns the start of this month as a ZonedDateTime using a specific time zone.
      Parameters:
      zone - the time zone for the return value
      Returns:
      the start of this month
    • atEndOfMonth

      public LocalDateTime atEndOfMonth()
      Returns the end of this month as a LocalDateTime.
      Returns:
      the end of this month
    • atEndOfMonth

      public ZonedDateTime atEndOfMonth(ZoneId zone)
      Returns the end of this month as a ZonedDateTime 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

      public Stream<LocalMonth> monthsUntil(LocalMonth endExclusive)
      Returns a sequential ordered stream of months. The returned stream starts from this month (inclusive) and goes to endExclusive (exclusive) by an incremental step of 1 month.
      Parameters:
      endExclusive - the end month, exclusive, not null
      Returns:
      a sequential Stream for the range of LocalMonth values
      Throws:
      IllegalArgumentException - if end month is before this month
    • plusMonths

      public LocalMonth plusMonths(int monthsToAdd)
      Returns a copy of this LocalMonth 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

      public LocalMonth plusYear(int yearsToAdd)
      Returns a copy of this LocalMonth 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

      public LocalMonth minusMonths(int monthsToSubtract)
      Returns a copy of this LocalMonth 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

      public LocalMonth minusYear(int yearsToSubtract)
      Returns a copy of this LocalMonth 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

      public LocalMonth nextMonth()
      Returns the subsequent (following) month.
      Returns:
      the next month
    • previousMonth

      public LocalMonth previousMonth()
      Returns the previous month
      Returns:
      the month before.
    • withMonth

      public LocalMonth withMonth(int month)
      Returns a copy of this LocalMonth 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

      public LocalMonth withYear(int year)
      Returns a copy of this LocalMonth 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

      public int compareTo(LocalMonth otherMonth)
      Specified by:
      compareTo in interface Comparable<LocalMonth>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object