[HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by OCAJO1 »

You should change the second line to ld = ld.plusMonths(2).plusDays(10); if you want to see 2018-02-10 in the output.
Should be

2018-03-11.

Also,
LocalDate ld = LocalDate.of(2018, 1, 1);
ld = ld.withYear(2010).plusDays(10).atTime(10, 20).plusMonths(2);
The above example of what doesn't work, has two tricks in it. One, atTime() returns LocalDateTime and two, LocalDateTime does not have atTime() method.

Do the date and time questions on the exam get this tricky? Thanks

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by admin »

1. Correct.
2. Yes, you may see this on the exam.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by Username987654 »

Thus, LocalDate.of(2018, 1, 31) produces a new LocalDate instance and chaining another of method to this LocalDate instance is actually the same as invoking LocalDate.of(2019, 1, 31) independently.
should be
Thus, LocalDate.of(2018, 1, 31) produces a new LocalDate instance and chaining another of method to this LocalDate instance is actually the same as invoking LocalDate.of(2019, 1, 1) independently.
?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by admin »

Correct. Should be fixed.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by Username987654 »

The boook shows the following code and quote:

Code: Select all

LocalDate ld = LocalDate.of(2018, 1, 1);
ld.plusHours(10); // will not compile
Creating a date/time object using an existing date/time object
For example, adding hours to a LocalDate or adding months to a LocalTime, will throw a DateTimeException at runtime.
Typo? Or can both of these be true at the same time? If so, what's the best way to remember what happens in each case?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by admin »

This seems fine because it is explaining the general pattern of these methods i.e. creating a new object using an existing one but it also notes the limitations of these methods.

LocalDate has only the date component (no time), so, if you try to add time components to it (i.e. hours, mins, or secs) to LocalDate, it will throw an exception.
Similarly, LocalTime has only the time component (no date), so, if you try to add date components to it (i.e. days, months, year) to LocalTime, it will throw an exception.

Only LocalDateTime has both the date and the time components, so, you can add date as well as time components to a LocalDateTime object.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by Username987654 »

It's just that from reading (alone), it seems like a possible typo, or maybe a point that could potentially be made more clear, that would allow me to answer a potential exam question correctly since "adding hours to a LocalDate" results sometimes in a compile error, and other times as a DateTImeException?

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by Username987654 »

(Maybe I could use some sleep.)

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by admin »

OK, I see your point.

LocalDate does not have methods to add time components that is why there is a compilation error if you try to call addHours on a LocalDate reference. (Same with LocalTime, which does not have methods to add the date components).

However, all of them (i.e. LocalDate, LocalTime, and LocalDateTIme) have plus/minus(TemporalAmount ) methods. If you use these method, the compiler cannot check what are you trying to add/subtract. So, the compilation succeeds but an exception will be thrown at run time when you do something like:
LocalDate ld = LocalDate.now();
ld.plus(Duration.ofHours(1)); //no compilation error but exception at run time. java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]

Post by Username987654 »

Perfect! Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests