232.
233.
CHAPTER
05
DateTime API
Explain what is wrong in the following code snippet and
how you can fix it.
LocalDate date1 = LocalDate.of(2018, 7, 24);
LocalTime time1 = date1.atTime(5, 30);
Answer:
The code snippet above first
creates a LocalDate called
date1. It then uses the atTime() method on date1 and
assigns the result to a LocalTime object called time1. This
causes a compilation error. The atTime() method combines the
date in the current time object with the
specified time and returns
a LocalDateTime object. So, you can fix the code above by
making the following change:
LocalDateTime time1 = date1.atTime(5, 30);
This combines the date specified in date1 with
the time 5:30 and
returns a LocalDateTime object.
Explain with appropriate code samples the different ways
in which you can create a LocalDate.
Answer:
The LocalDate class has some
static methods using for
LocalDate creation. These are as follows:
a.
b.
c.
234.
Creating a LocalDate corresponding to the current Date– The
LocalDate.now() method can be used for this as shown
below:
LocalDate date1 = LocalDate.now();
Creating a LocalDate corresponding
to the specified year,
month and day– The LocalDate.of method can be used
for this as shown below:
LocalDate date2 = LocalDate.of(2018, 7, 24);
Creating a LocalDate corresponding to a String Date– The
LocalDate.parse can be used for this as shown below:
LocalDate date3 = LocalDate.parse(“2014–08–11”);
Do'stlaringiz bilan baham: