Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet76/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   72   73   74   75   76   77   78   79   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Sometimes it is necessary to obtain the current system time. The LocalTime class 
obtains the current time by calling its now() method. Similar to the LocalDate class, the 
LocalTime.now() method can be called to return a LocalTime object that is equal to the 
current time. The LocalTime class also contains several methods that can be utilized to 
manipulate the time. The examples contained in the solution provide a brief overview of 
the available methods.
Let’s look at a handful of examples to provide some context for how the LocalTime 
methods are invoked. To obtain a LocalTime object set to a specific time, invoke the 
LocalTime.of(int, int) method, passing int parameters representing the hour 
and minute.
// of(int hours, int min): obtain a specific time
LocalTime pastTime = LocalTime.of(1, 10);
The atDate(LocalDate) instance method applies a LocalDate object to a LocalTime 
instance, returning a LocalDateTime object.
LocalDateTime ldt = time.atDate(LocalDate.of(2021,Month.NOVEMBER,12);
There are several methods that can obtain portions of the time. For instance, the 
getHour(), getMinute(), getNano(), and getSecond() methods return those specified 
portions of the LocalTime object.
int hour = time.getHour();
int min = time.getMinute();
int nano = time.getNano();
int sec = time.getSecond();
Several comparison methods are also available for use. For example, the 
compareTo(LocalTime) method compares one LocalTime object to another. 
isAfter(LocalTime) determines if the time is after another, and isBefore(LocalTime) 
specifies the opposite.
Chapter 4 Numbers aNd dates


126
4-11. Obtaining and Using the Date and Time
Together
 Problem
In your application, you want to display the current date and the current time.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   72   73   74   75   76   77   78   79   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling