Class Day
- All Implemented Interfaces:
Comparable<Day>
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
ConstructorsConstructorDescriptionDay()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.Constructs a Day by copying another Day instance.Constructs a Day from milliseconds since epoch.Constructs a Day from aLocalDate.Constructs a Day from a Calendar instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDays(int days) Adds the specified number of days to this Day.voidaddMonths(int months) Adds the specified number of months to this Day.voidaddYears(int years) Adds the specified number of years to this Day.booleanafter(long date) Checks if this Day is after the specified date.booleanChecks if this Day is after another Day.booleanbefore(long date) Checks if this Day is before the specified date.booleanChecks if this Day is before another Day.intCompares this Day to another Day chronologically.booleanChecks if this Day is equal to another object.intReturns the day of month component of this Day.intReturns 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 StringgetDayOfWeekString(int dayOfWeek) Returns the day of week name for a given day number.intReturns the day of week for the first day of this month.static DaygetForUtcOffset(int utcOffset) Creates a Day for the current date adjusted by a UTC offset.intReturns the last day of this month.Converts this Day to a JavaLocalDate.intgetMonth()Returns the month component of this Day.Returns the three-letter abbreviation of the month name.Returns the full name of the month.Returns a formatted string representation of this Day.longgetTime()Returns this Day as milliseconds since epoch.intgetWeek()Returns the week of year for this Day.intgetYear()Returns the year component of this Day.inthashCode()Returns a hash code for this Day.voidset(int year, int month, int day) Sets this Day to the specified year, month, and day.voidset(long dateMsecs) Sets this Day from milliseconds since epoch.voidSets this Day by copying from another Day instance.toString()Returns a string representation of this Day using the default format.
-
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
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
Constructs a Day by copying another Day instance.- Parameters:
day- the Day to copy
-
Day
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
Constructs a Day from aLocalDate.- Parameters:
date- the LocalDate to wrap
-
-
Method Details
-
getForUtcOffset
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
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
Returns the full name of the month.- Returns:
- the month name (e.g., "January", "February")
-
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
Returns the full name of the day of week.- Returns:
- the day name (e.g., "Sunday", "Monday")
-
getDayOfWeekAbbr
Returns the two-letter abbreviation of the day of week name.- Returns:
- the day abbreviation (e.g., "Su", "Mo")
-
getDayOfWeekString
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
Converts this Day to a JavaLocalDate.- 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
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
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
Returns a string representation of this Day using the default format.The default format is "YYYY-MM-DD" (ISO 8601).
-
getString
Returns a formatted string representation of this Day.Internally, every
Min 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
Checks if this Day is equal to another object.Two Days are equal if they have the same year, month, and day.
-
hashCode
public int hashCode()Returns a hash code for this Day. -
compareTo
Compares this Day to another Day chronologically.- Specified by:
compareToin interfaceComparable<Day>- Parameters:
other- the Day to compare to- Returns:
- negative if this Day is before other, positive if after, zero if equal
-