Program
import java.time.LocalDate; import java.time.OffsetDateTime; public class Offset { public static void main(String[] args) { // TODO Auto-generated method stub OffsetDateTime offsetDT = OffsetDateTime.now(); System.out.println("day of month :" +offsetDT.getDayOfMonth()); OffsetDateTime offsetDT1 = OffsetDateTime.now(); System.out.println("day of year :" +offsetDT1.getDayOfYear()); OffsetDateTime offsetDT2 = OffsetDateTime.now(); System.out.println("day of week :" +offsetDT2.getDayOfWeek()); OffsetDateTime offsetDT3 = OffsetDateTime.now(); System.out.println("date of today :" +offsetDT3.toLocalDate()); LocalDate date = LocalDate.now(); LocalDate yesterday = date.minusDays(1); LocalDate tomorrow = yesterday.plusDays(2); System.out.println("Yesterday date: "+yesterday); System.out.println("Tommorow date: "+tomorrow); } }
Output
day of month :8 day of year :98 day of week :THURSDAY date of today :2021-04-08 Yesterday date: 2021-04-07 Tommorow date: 2021-04-09
Description
public static OffsetDateTime now()
Obtains the current date-time 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-time.
The offset will be calculated from the time-zone in the clock.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
Returns:
the current date-time using the system clock, not null
public int getDayOfMonth()
Gets the day-of-month field.
This method returns the primitive int value for the day-of-month.
Returns:
the day-of-month, from 1 to 31
public int getDayOfYear()
Returns:
Gets the day-of-year field.
This method returns the primitive int value for the day-of-year.
Returns:
the day-of-year, from 1 to 365, or 366 in a leap year
public DayOfWeek getDayOfWeek()
Gets the day-of-week field, which is an enum DayOfWeek.
This method returns the enum DayOfWeek for the day-of-week. 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.
Additional information can be obtained from the DayOfWeek. This includes textual names of the values.
Returns:
the day-of-week, not null
public static LocalDate now()
Obtains the current date 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 date using the system clock and default time-zone, not null
public LocalDate minusDays​(long daysToSubtract)
Returns a copy of this LocalDate with the specified number of days subtracted.
This method subtracts the specified amount from the days field decrementing the month and
year fields as necessary to ensure the result remains valid. The result is only invalid if the
maximum/minimum year is exceeded.
For example, 2009-01-01 minus one day would result in 2008-12-31.
This instance is immutable and unaffected by this method call.
Parameters:
daysToSubtract – the days to subtract, may be negative
Returns:
a LocalDate based on this date with the days subtracted, not null
Throws:
DateTimeException – if the result exceeds the supported date range
public LocalDate plusDays​(long daysToAdd)
Returns a copy of this LocalDate with the specified number of days added.
This method adds the specified amount to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.
For example, 2008-12-31 plus one day would result in 2009-01-01.
This instance is immutable and unaffected by this method call.
Parameters:
daysToAdd – the days to add, may be negative
Returns:
a LocalDate based on this date with the days added, not null
Throws:
DateTimeException – if the result exceeds the supported date range