Page 1 of 1

Question enthuware.ocpjp.v7.2.1192:

Posted: Mon Jun 13, 2016 10:17 am
by zhajikun
How will you initialize a SimpleDateFormat object so that the following code will print the full text time zone of the given date?

System.out.println(sdf.format(new Date()));

A. SimpleDateFormat sdf = new SimpleDateFormat("tttt", Locale.FRANCE);

B. SimpleDateFormat sdf = new SimpleDateFormat("T", Locale.FRANCE);

C. SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.FRANCE);

D. SimpleDateFormat sdf = new SimpleDateFormat("zzzz", Locale.FRANCE);

E. SimpleDateFormat sdf = new SimpleDateFormat("XXX", Locale.FRANCE);

The given answer is D, however, when I type in D in netbeans, it give me an compile error.

C is correct according to Netbeans. It prints out "CDT".

Re: Question enthuware.ocpjp.v7.2.1192:

Posted: Mon Jun 13, 2016 10:53 am
by admin
Please try using command line. Option D is correct.

HTH,
Paul.

Question enthuware.ocpjp.v7.2.1192:

Posted: Tue Jul 04, 2017 4:06 am
by dominik.jenik
In the explanation note under answer of question states:
Remember that if the number of pattern letters is 4 or more, the full form is used;
When I try this:

Code: Select all

System.out.println(new SimpleDateFormat("XXX", Locale.FRANCE).format(new Date()));
everything looks good, but when I try this:

Code: Select all

System.out.println(new SimpleDateFormat("XXXX", Locale.FRANCE).format(new Date()));
exception occurs. This looks like "hidden feature" of Java compiler.

Code: Select all

Exception in thread "main" java.lang.IllegalArgumentException: invalid ISO 8601 format: length=4
	at java.text.SimpleDateFormat.encode(SimpleDateFormat.java:878)
	at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:863)
	at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:659)
	at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
	at Runner.main(Runner.java:30)

Re: Question enthuware.ocpjp.v7.2.1192:

Posted: Tue Jul 04, 2017 5:50 am
by admin
That explanation is for option 4. i.e. zzzz not XXXX.