Class Day

java.lang.Object
com.oorian.utils.Day
All Implemented Interfaces:
Comparable<Day>

public class Day extends Object implements Comparable<Day>
Represents a specific calendar day (year, month, and day) at midnight (00:00:00) UTC.

Day is a lightweight wrapper around LocalDate used for user-selected dates that must remain independent of time zones. No matter where in the world this date is viewed, the year, month, and day values will not change.

Note: Month values use 0-based indexing (0=January through 11=December) for backward compatibility with Calendar.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Day()
    Constructs a Day representing today's date in UTC.
    Day(int year, int month, int day)
    Constructs a Day with the specified year, month, and day.
    Day(Day day)
    Constructs a Day by copying another Day instance.
    Day(Long dateMsecs)
    Constructs a Day from milliseconds since epoch.
    Day(LocalDate date)
    Constructs a Day from a LocalDate.
    Day(Calendar calendar)
    Constructs a Day from a Calendar instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addDays(int days)
    Adds the specified number of days to this Day.
    void
    addMonths(int months)
    Adds the specified number of months to this Day.
    void
    addYears(int years)
    Adds the specified number of years to this Day.
    boolean
    after(long date)
    Checks if this Day is after the specified date.
    boolean
    after(Day date)
    Checks if this Day is after another Day.
    boolean
    before(long date)
    Checks if this Day is before the specified date.
    boolean
    before(Day date)
    Checks if this Day is before another Day.
    int
    compareTo(Day other)
    Compares this Day to another Day chronologically.
    boolean
    Checks if this Day is equal to another object.
    int
    Returns the day of month component of this Day.
    int
    Returns the day of week for this Day.
    Returns the two-letter abbreviation of the day of week name.
    Returns the full name of the day of week.
    static String
    getDayOfWeekString(int dayOfWeek)
    Returns the day of week name for a given day number.
    int
    Returns the day of week for the first day of this month.
    static Day
    getForUtcOffset(int utcOffset)
    Creates a Day for the current date adjusted by a UTC offset.
    int
    Returns the last day of this month.
    Converts this Day to a Java LocalDate.
    int
    Returns the month component of this Day.
    Returns the three-letter abbreviation of the month name.
    Returns the full name of the month.
    getString(String dateFormat)
    Returns a formatted string representation of this Day.
    long
    Returns this Day as milliseconds since epoch.
    int
    Returns the week of year for this Day.
    int
    Returns the year component of this Day.
    int
    Returns a hash code for this Day.
    void
    set(int year, int month, int day)
    Sets this Day to the specified year, month, and day.
    void
    set(long dateMsecs)
    Sets this Day from milliseconds since epoch.
    void
    set(Day day)
    Sets this Day by copying from another Day instance.
    Returns a string representation of this Day using the default format.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Day

      public Day(int year, int month, int day)
      Constructs a Day with the specified year, month, and day.
      Parameters:
      year - the year (e.g., 2025)
      month - the month (0-11, where 0 is January)
      day - the day of month (1-31)
    • Day

      public Day()
      Constructs a Day representing today's date in UTC.
    • Day

      public Day(Long dateMsecs)
      Constructs a Day from milliseconds since epoch.

      The milliseconds value is interpreted in UTC timezone.

      Parameters:
      dateMsecs - milliseconds since January 1, 1970 00:00:00 UTC
    • Day

      public Day(Day day)
      Constructs a Day by copying another Day instance.
      Parameters:
      day - the Day to copy
    • Day

      public Day(Calendar calendar)
      Constructs a Day from a Calendar instance.

      Extracts the year, month, and day from the calendar.

      Parameters:
      calendar - the Calendar to extract the date from
    • Day

      public Day(LocalDate date)
      Constructs a Day from a LocalDate.
      Parameters:
      date - the LocalDate to wrap
  • Method Details

    • getForUtcOffset

      public static Day getForUtcOffset(int utcOffset)
      Creates a Day for the current date adjusted by a UTC offset.

      This is useful when you need to determine what day it is for a user in a different timezone.

      Parameters:
      utcOffset - the offset from UTC in minutes (e.g., -300 for EST)
      Returns:
      a Day representing the current date in the specified timezone
    • set

      public void set(long dateMsecs)
      Sets this Day from milliseconds since epoch.

      The milliseconds value is interpreted in UTC timezone.

      Parameters:
      dateMsecs - milliseconds since January 1, 1970 00:00:00 UTC
    • set

      public void set(int year, int month, int day)
      Sets this Day to the specified year, month, and day.
      Parameters:
      year - the year (e.g., 2025)
      month - the month (0-11, where 0 is January)
      day - the day of month (1-31)
    • set

      public void set(Day day)
      Sets this Day by copying from another Day instance.
      Parameters:
      day - the Day to copy from
    • addYears

      public void addYears(int years)
      Adds the specified number of years to this Day.

      Negative values subtract years.

      Parameters:
      years - the number of years to add (or subtract if negative)
    • addMonths

      public void addMonths(int months)
      Adds the specified number of months to this Day.

      Negative values subtract months. The day is adjusted if it would exceed the number of days in the resulting month.

      Parameters:
      months - the number of months to add (or subtract if negative)
    • addDays

      public void addDays(int days)
      Adds the specified number of days to this Day.

      Negative values subtract days. Month and year boundaries are handled automatically.

      Parameters:
      days - the number of days to add (or subtract if negative)
    • getYear

      public int getYear()
      Returns the year component of this Day.
      Returns:
      the year (e.g., 2025)
    • getMonth

      public int getMonth()
      Returns the month component of this Day.
      Returns:
      the month (0-11, where 0 is January)
    • getDayOfMonth

      public int getDayOfMonth()
      Returns the day of month component of this Day.
      Returns:
      the day of month (1-31)
    • getMonthName

      public String getMonthName()
      Returns the full name of the month.
      Returns:
      the month name (e.g., "January", "February")
    • getMonthAbbr

      public String getMonthAbbr()
      Returns the three-letter abbreviation of the month name.
      Returns:
      the month abbreviation (e.g., "Jan", "Feb")
    • getFirstWeekdayOfMonth

      public int getFirstWeekdayOfMonth()
      Returns the day of week for the first day of this month.

      Useful for calendar rendering to determine where the month starts.

      Returns:
      the day of week (1=Sunday through 7=Saturday)
    • getLastDayOfMonth

      public int getLastDayOfMonth()
      Returns the last day of this month.

      Accounts for varying month lengths and leap years.

      Returns:
      the last day of the month (28, 29, 30, or 31)
    • getDayOfWeek

      public int getDayOfWeek()
      Returns the day of week for this Day.
      Returns:
      the day of week (1=Sunday through 7=Saturday)
    • getWeek

      public int getWeek()
      Returns the week of year for this Day.
      Returns:
      the week number (1-52 or 1-53)
    • getDayOfWeekName

      public String getDayOfWeekName()
      Returns the full name of the day of week.
      Returns:
      the day name (e.g., "Sunday", "Monday")
    • getDayOfWeekAbbr

      public String getDayOfWeekAbbr()
      Returns the two-letter abbreviation of the day of week name.
      Returns:
      the day abbreviation (e.g., "Su", "Mo")
    • getDayOfWeekString

      public static String getDayOfWeekString(int dayOfWeek)
      Returns the day of week name for a given day number.

      Handles values outside the 1-7 range by wrapping.

      Parameters:
      dayOfWeek - the day of week number (1=Sunday through 7=Saturday)
      Returns:
      the day name (e.g., "Sunday", "Monday")
    • getLocalDate

      public LocalDate getLocalDate()
      Converts this Day to a Java LocalDate.
      Returns:
      a LocalDate representing this Day
    • getTime

      public long getTime()
      Returns this Day as milliseconds since epoch.

      The time is at midnight UTC.

      Returns:
      milliseconds since January 1, 1970 00:00:00 UTC
    • before

      public boolean before(long date)
      Checks if this Day is before the specified date.
      Parameters:
      date - the date to compare against (milliseconds since epoch)
      Returns:
      true if this Day is before the specified date
    • before

      public boolean before(Day date)
      Checks if this Day is before another Day.
      Parameters:
      date - the Day to compare against
      Returns:
      true if this Day is before the specified Day
    • after

      public boolean after(long date)
      Checks if this Day is after the specified date.
      Parameters:
      date - the date to compare against (milliseconds since epoch)
      Returns:
      true if this Day is after the specified date
    • after

      public boolean after(Day date)
      Checks if this Day is after another Day.
      Parameters:
      date - the Day to compare against
      Returns:
      true if this Day is after the specified Day
    • toString

      public String toString()
      Returns a string representation of this Day using the default format.

      The default format is "YYYY-MM-DD" (ISO 8601).

      Overrides:
      toString in class Object
      Returns:
      the formatted date string
    • getString

      public String getString(String dateFormat)
      Returns a formatted string representation of this Day.

      Internally, every M in the format string is doubled before token replacement. The tokens listed below are what the caller writes in the format string.

      Format Tokens:

      • YYYY - Four-digit year (e.g., "2025")
      • YY - Two-digit year (e.g., "25")
      • MMMM - Full month name (e.g., "January")
      • MMM - Three-letter month abbreviation (e.g., "Jan")
      • MM - Two-digit month with leading zero (e.g., "01")
      • M - Month without leading zero (e.g., "1")
      • DDDD - Full day of week name (e.g., "Monday")
      • DDD - Three-letter day abbreviation (e.g., "Mon")
      • DD - Two-digit day with leading zero (e.g., "05")
      • D - Day without leading zero (e.g., "5")
      Parameters:
      dateFormat - the format pattern, or null for default "YYYY-MM-DD"
      Returns:
      the formatted date string
    • equals

      public boolean equals(Object obj)
      Checks if this Day is equal to another object.

      Two Days are equal if they have the same year, month, and day.

      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare against
      Returns:
      true if the objects represent the same day
    • hashCode

      public int hashCode()
      Returns a hash code for this Day.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code based on year, month, and day
    • compareTo

      public int compareTo(Day other)
      Compares this Day to another Day chronologically.
      Specified by:
      compareTo in interface Comparable<Day>
      Parameters:
      other - the Day to compare to
      Returns:
      negative if this Day is before other, positive if after, zero if equal