Page 1 of 1
[HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Tue Apr 23, 2019 6:19 pm
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
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Tue Apr 23, 2019 9:15 pm
by admin
1. Correct.
2. Yes, you may see this on the exam.
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Thu Jun 13, 2019 9:18 am
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.
?
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Thu Jun 13, 2019 9:54 pm
by admin
Correct. Should be fixed.
thank you for your feedback!
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Mon Sep 09, 2019 12:08 am
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?
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Mon Sep 09, 2019 12:14 am
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.
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Mon Sep 09, 2019 12:34 am
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?
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Mon Sep 09, 2019 12:35 am
by Username987654
(Maybe I could use some sleep.)
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Mon Sep 09, 2019 1:42 am
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
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Mon Sep 09, 2019 8:29 am
by Username987654
Perfect! Thanks!
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Thu Jun 13, 2024 10:00 am
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
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Thu Jun 13, 2024 11:38 pm
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.
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Thu Jun 13, 2024 11:52 pm
by admin
5. Also, parsing and formatting date such as: LocalDate.parse("2024-06-14", DateTimeFormatter.ISO_DATE);
Re: [HD Pg 351, Sec. 12.3.6 - chaining-method-calls]
Posted: Fri Jun 14, 2024 2:07 am
by raphaelzintec
this is much better thankx a lot!