Java Reference
In-Depth Information
2-5. Incorporating the Current Date and
Time
Problem
You want to obtain the current date and time for use in your application.
Solution
Utilize the new Date-Time API to retrieve the current date and time. The following
code demonstrates how to do so.
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
How It Works
The Date-Time API makes it easy to obtain the current date and time, each as separate
entities. To do so, you import the java.time.LocalDate and
java.time.LocalTime classes and call upon their static now() methods to re-
trieve instances of the local date and time, respectively. Both the LocalDate and the
LocalTime classes cannot be instantiated, as they are immutable and thread-safe. To
learn more, refer to Recipe 4-7.
2-6. Obtaining a Date from a Specified
String
Problem
You want to obtain a LocalDate object, given a specified string of text that contains
a date.
Search WWH ::




Custom Search