120
Another version of the now() method accepts a java.time.Clock object as a
parameter and returns the date based on that clock. For instance,
the following lines of
code demonstrate how to obtain a Clock object that represents the system time.
public static void newDateFromClock() {
Clock clock = Clock.systemUTC();
LocalDate date = LocalDat.now(clock);
System.out.println("Date from clock: " + date);
}
The output is:
Date from clock: 2021-11-30
The main class is:
public static void main(String[] args) {
newDate();
newDateFromClock();
}
There were other ways to obtain the current date in previous releases, but usually,
the
time came with the date, and then formatting had to be done to remove the
unneeded time digits. The new java.time.LocalDate class
makes it possible to work
with dates separate from times.
Do'stlaringiz bilan baham: