Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed Nov 25, 2015 7:16 am
by Mushfiq Mammadov

Option statement is clear, the code does still compile without changing LocalDateTime to ZonedDateTime. But I can't understand explanation maybe for my english. Is it regarding LocalDateTime parameter or ZonedDateTime? Because the code will work if we change LocalDateTime to ZonedDateTime:
Code: Select all
class TestZonedDateTime {
public static void main(String[] args) {
TestZonedDateTime t = new TestZonedDateTime();
System.out.println(t.getDateString(ZonedDateTime.now()));
}
public String getDateString(ZonedDateTime ldt) {
return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(ldt); // 2015-11-25T16:10:42.794+04:00[Asia/Baku]
}
}
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed Nov 25, 2015 9:47 am
by admin
If you pass a LocalDateTime and try to format it with ISO_ZONED_DATE_TIME, it will throw an exception at run time. That is why the explanation says that the given code will not work at run time. Of course, the code can be fixed. But the explanation is talking about the code as presented in the problem statement.
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Thu Nov 26, 2015 8:19 am
by Mushfiq Mammadov
admin wrote:If you pass a LocalDateTime and try to format it with ISO_ZONED_DATE_TIME, it will throw an exception at run time. That is why the explanation says that the given code will not work at run time.
This had already been written in the explanation of option 1 therefore I guessed the underlined part was regarding ZonedDateTime parameter.
Thanks for the reply
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Tue Feb 28, 2017 10:49 am
by actuaris
Zoned date times are not part of the OCA exam. So I gambled for option 4. Since my exam is next week, do I have to look at things zoned dates as well?
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Tue Feb 28, 2017 10:42 pm
by admin
No, you don't need to read any thing more about ZonedDateTime. Whatever information is given in this question's explanation is enough.
Just remember that LocalDateTime class does not contain Zone information but if you format a local date using ISO_ZONED_DATE_TIME formatter, then there will be an exception because this formatter requires zone information.
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed May 24, 2017 1:06 pm
by zserrbo
I think the fourth answer's explanation is not fully correct.
option four:
The method parameter type must be changed from LocalDateTime to ZonedDateTime for it to compile.
and its explanation:
Although it is true that this code will never work at runtime, it will compile fine as it is.
I understand that the method parameter changing from LocalDateTime to ZonedDateTime it's not neccessary and thus the option one is the answer. But the code will works at runtime if we change the method parameter type (and of course we should add to ZoneDateTime to method argument).
Am I correct?
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Mon Aug 28, 2017 4:25 pm
by nickimix
zserrbo wrote:I think the fourth answer's explanation is not fully correct.
option four:
The method parameter type must be changed from LocalDateTime to ZonedDateTime for it to compile.
and its explanation:
Although it is true that this code will never work at runtime, it will compile fine as it is.
I understand that the method parameter changing from LocalDateTime to ZonedDateTime it's not neccessary and thus the option one is the answer. But the code will works at runtime if we change the method parameter type (and of course we should add to ZoneDateTime to method argument).
Am I correct?
Agree with you. This variant and explanation is not clear.
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Mon Aug 28, 2017 10:26 pm
by admin
I am not sure what is not clear. The option says that you must do X for it to compile.
The explanation says that it will compile without any change.
The explanation also says that the given code will not work at run time even though it will compile fine.
Now, you are saying that it will compile and run fine if the code is changed to something else. Yes, there could be multiple ways to make the code compile and run fine. But the explanation is talking about the code that is given.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed Nov 28, 2018 4:15 pm
by flex567
In the book the only use of DateTimeFormatter class is like this:
The way you are using it is by calling the inner class (which should not be on the exam)
Code: Select all
DateTimeFormatter.ISO_ZONED_DATE_TIME.format(ldt);
Can you maybe explain a bit about the use of it, just in case we encounter question like that?
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed Dec 12, 2018 11:24 am
by raj_dp
The option number 3 seems to be correct because when we change from LocalDateTime to ZonedDateTime it compiles and runs properly.
ZonedDateTime zdt1 = ZonedDateTime.now();
System.out.println(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt1)); //output: 2018-12-12T21:51:59.931+05:30[Asia/Calcutta]
I dont understand why it has been said that it will never work at runtime.
Regards
Raj
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed Dec 12, 2018 11:29 am
by admin
raj_dp wrote: ↑Wed Dec 12, 2018 11:24 am
The option number 3 seems to be correct because when we change from LocalDateTime to ZonedDateTime it compiles and runs properly.
I dont see your code showing that it compiles and runs properly!!
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Wed Dec 12, 2018 11:30 am
by admin
Also, option 3 says the given code wont complie as it is. Did you try to compile it?
Please go through the discussion above.
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Thu Dec 13, 2018 12:31 am
by raj_dp
I can see that the option 3 of Question number 63 of Test 6 say - "The method parameter type must be changed from LocalDateTime to ZonedDateTime for it to compile." So, I tried out the following:
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
public class Q_63 {
public static void main(String[] args) {
ZonedDateTime zdt1 = ZonedDateTime.now();
System.out.println(getDateString(zdt1));
}
public static String getDateString(ZonedDateTime zdt){
return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt);
}
}
This compiles and runs fine and the output is: 2018-12-13T10:52:47.093+05:30[Asia/Calcutta]
Regards
Raj
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Thu Dec 13, 2018 12:50 am
by admin
Sorry, but I am really not sure what is the issue. To see whether the given option is correct or not you have to compile the code that is given. Does the given code compile? If yes, then the option is wrong because the option says the code must be changed to compile. But no change is required to make the code compile. The explanation also says the same thing...the given code will compile but it will throw an exception at run time. So, I don't know what you are trying to show by your example. The same thing is also explained in older posts. You need to go through the thread.
Re: About Question enthuware.ocajp.i.v8.2.1434 :
Posted: Thu Dec 13, 2018 2:18 am
by raj_dp
Oh I am so sorry. I got it now. That option is wrong because even without changing the parameter from LocalDateTime to ZonedDateTime, the given program will compile.
Thanks for patiently explaining.
Regards
Raj