[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

Online
admin
Site Admin
Posts: 10386
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.

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.
?

Online
admin
Site Admin
Posts: 10386
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!

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?

Online
admin
Site Admin
Posts: 10386
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.

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.)

Online
admin
Site Admin
Posts: 10386
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

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!

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

Hello

This localdate part of chapter 12 must be an entire chapter

and in other OCA books this part was very short

the thing is that i understood everything about the book of mister deskmush but this LOCALdate has nothing to do with logic that's why i feel extremly bored reading it, this is the only part of the entire book giving me zero motivation. This date is more into memorizing bunch of different syntaxes and i hate this.

So i'm about to use my only joker for this part, i mean still doing exam without knowing wel this part

i'd appreciate to get your opinion about this and also if you can tell me what's the minimum to know about localdate lol

thanks in advance

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

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

Post by admin »

It is true. The Date stuff is very boring and requires a lot of memorization.

At least, learn the following the things:

1. Immutability (all new date classes are immutable),
2. How LocalDate.of method works. What happens if you pass an invalid value an an argument such as 13 for month or 30 for day of when month is February. (DateTimeException)
3. LocalDate's plusDays and other plus method. (See how month changes).
4. Comparing dates - isAfter, isBefore.

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

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

Post by admin »

5. Also, parsing and formatting date such as: LocalDate.parse("2024-06-14", DateTimeFormatter.ISO_DATE);

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

this is much better thankx a lot!

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests