Java Reference
In-Depth Information
java.util.Calendar get() method. When some method or class is deprec-
ated, that means it should no longer be used because it might be removed in some fu-
ture 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 will most likely be
included in future releases of Java. The methods that were left intact include the com-
parison methods after(), before(), compareTo(), setTime() , and
equals() . Solution #2 to this recipe demonstrates how to instantiate a Date object
and print out the current date and time.
As mentioned previously, the Date class has many methods that have become de-
precated 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 that is included in the Date class,
except the Calendar class is much more flexible. The Calendar class is actually
an abstract class that contains methods that are used for converting between a specific
time and date, and manipulating the calendar in various ways. The Calendar , as
demonstrated in solution #3, is one such class that extends the Calendar class and
therefore provides this functionality. The Calendar class has gained a few new meth-
ods in Java 8. The new methods in java.util.Calendar are listed in Table 4-6 .
Table 4-6 . New Methods for java.util.Calendar in Java 8
Method Name
Description
Returns un-modifiable set containing all supported
calendar types.
getAvailableCalendarTypes()
Returns the calendar type of this calendar.
getCalendarType()
Converts to an instant.
toInstant()
For some applications, the Date class will work fine. For instance, the Date class
can be useful when working with timestamps. However, if the application requires de-
tailed 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.
 
 
Search WWH ::




Custom Search