Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet79/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   75   76   77   78   79   80   81   82   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Many applications require the use of the current calendar date. It is often also necessary 
to obtain the current time. There are different ways to do that, and the solution to this 
recipe demonstrates three of them. The Date-Time API includes a LocalDateTime class 
that enables you to capture the current date and time by invoking its now() method. 
A specified date and time can be obtained by specifying the corresponding int and 
month type parameters when calling LocalDateTime.of(). There are also a multitude 
of methods available for use via a LocalDateTime instance, such as getHours(), 
getMinutes(), getNanos(), and getSeconds(), which allow for finer-grained 
control of the date and time. An instance of LocalDateTime also contains methods for 
performing calculations, conversions, comparisons, and more. Solution 1 to this recipe 
demonstrates the use of the LocalDateTime, showcasing how to perform calculations 
and obtain portions of the date and time for further use.
By default, the java.util.Date class can be instantiated with no arguments to 
return the current date and time. The Date class can also return the current time via 
the getTime() method. As mentioned in the solution, the getTime() method returns 
the number of milliseconds since January 1, 1970, 00:00:00 GMT, represented by the 
Chapter 4 Numbers aNd dates


131
Date object that is in use. Several other methods can be called against a Date object 
with regard to breaking down the current date and time into more granular intervals. 
For instance, the Date class has the getHours(), getMinutes(), getSeconds(), 
getMonth(), getDay(), getTimezoneOffset(), and getYear() methods. However, 
it is not advisable to use any of these methods, except getTime(), because each has 
been deprecated using java.time.LocalDateTime and the java.util.Calendar get() 
methods. When a method or class is deprecated, it should no longer be used because 
it might be removed in some future release of the Java language. However, a few of the 
methods contained within the Date class have not been tagged as deprecated, so the 
Date class most likely be included in future releases of Java. The methods that were 
left intact include the after(), before(), compareTo(), setTime(), and equals() 
comparison methods. Solution 2 to this recipe demonstrates instantiating a Date object 
and printing out the current date and time.
As mentioned previously, the Date class has many methods that have become 
deprecated and should no longer be used. In solution 3 of this recipe, the java.util.
Calendar class is demonstrated as one successor for obtaining much of this information. 
The Calendar class was introduced in JDK 1.1, at which time many of the Date methods 
were deprecated. As you can see from solution 3, the Calendar class contains all the same 
functionality included in the Date class, except the Calendar class is much more flexible. 
The Calendar class contains methods used for converting between a specific time and 
date and manipulating the calendar in various ways. The Calendar class gained a few 
new methods in Java 8. One new method was java.util.
For some applications, the Date class works fine. For instance, the Date class can 
be useful when working with timestamps. However, if the application requires detailed 
manipulation of dates and times, then it is advisable to use a LocalDateTime or the 
Calendar class, which both include all the functionality of the Date class and more 
features as well. All solutions to this recipe are technically sound; choose the one that 
best suits the needs of your application.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   75   76   77   78   79   80   81   82   ...   245




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