Class YearMonthDay

java.lang.Object
com.norconex.commons.lang.time.YearMonthDay
All Implemented Interfaces:
Serializable, Comparable<YearMonthDay>

public final class YearMonthDay extends Object implements Comparable<YearMonthDay>, Serializable
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 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 year
      month - the month
      day - 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 year
      month - 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

      public YearMonthDay(@NonNull @NonNull Date date)
      Constructs a YearMonthDay from a Date (UTC).
      Parameters:
      date - a date
    • YearMonthDay

      public YearMonthDay(@NonNull @NonNull Calendar calendar)
      Constructs a YearMonthDay from a Calendar.
      Parameters:
      calendar - a calendar instant
    • YearMonthDay

      public YearMonthDay(@NonNull @NonNull String date)
      Constructs a YearMonthDay from a string of this format: yyyy-MM-dd.
      Parameters:
      date - a date string
    • YearMonthDay

      public YearMonthDay(@NonNull @NonNull LocalDate localDate)
      Constructs a YearMonthDay from a LocalDate.
      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

      public boolean isBefore(YearMonthDay ymd)
      Whether this YearMonthDay represents a date before the given YearMonthDay.
      Parameters:
      ymd - the YearMonthDay to evaluate
      Returns:
      true if before
    • isBeforeDate

      public boolean isBeforeDate(Date date)
      Whether this YearMonthDay represents a date before the given date.
      Parameters:
      date - the date to evaluate
      Returns:
      true if before
    • isAfter

      public boolean isAfter(YearMonthDay ymd)
      Whether this YearMonthDay represents a date after the given YearMonthDay.
      Parameters:
      ymd - the YearMonthDay to evaluate
      Returns:
      true if after
    • isAfterDate

      public boolean isAfterDate(Date date)
      Whether this YearMonthDay represents a date after the given date.
      Parameters:
      date - the date to evaluate
      Returns:
      true if after
    • contains

      public boolean contains(Date date)
      Whether this YearmMonthDay contains the given Date (i.e. same year, month, and day, regardless of time).
      Parameters:
      date - date to evaluate
      Returns:
      true if date is contained
    • toDate

      public Date toDate()
      Converts this YearMonthDay to a Date at 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

      public Date 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

      public YearMonthDay addDays(int numOfDays)
      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

      public YearMonthDay addMonths(int numOfMonths)
      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

      public YearMonthDay addYears(int numOfYears)
      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

      public Calendar toCalendar()
      Converts this YearMonthDay to a Calendar at midnight.
      Returns:
      a calendar
    • toLocalDate

      public LocalDate toLocalDate()
      Gets this YearMonthDay as a LocalDate.
      Returns:
      the local date
      Since:
      3.0.0
    • toLocalDateTime

      public LocalDateTime toLocalDateTime()
      Gets this YearMonthDay as a LocalDateTime at midnight (00:00).
      Returns:
      the local date time
      Since:
      3.0.0
    • compareTo

      public int compareTo(YearMonthDay ymd)
      Specified by:
      compareTo in interface Comparable<YearMonthDay>
    • toString

      public String toString()
      Converts the YearMonthDay to a string of this format: yyyy-MM-dd.
      Overrides:
      toString in class Object
    • toString

      public String toString(String pattern)
      Formats this YeamMonthDay using pattern build from SimpleDateFormat syntax, after first converting it to Date.
      Parameters:
      pattern - the pattern used for formatting
      Returns:
      the formatted string
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object