[HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

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 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by OCAJO1 »

Just so am clear about this. The sentence below (from when the section begins) is referring to when no formatter is passed to the parse method. Am I right?
Just like the no-args parse method,
Also, please clarify the following sentence from the end of the one-before-last paragraph of the section.
You only need to know how standardized formatters can be created.
Thanks

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by admin »

OCAJO1 wrote:
Mon Apr 22, 2019 1:58 pm
Just so am clear about this. The sentence below (from when the section begins) is referring to when no formatter is passed to the parse method. Am I right?
Just like the no-args parse method,
Yes, but it should really say something like just like the parse method with no formatter.
Also, please clarify the following sentence from the end of the one-before-last paragraph of the section.
You only need to know how standardized formatters can be created.
Thanks
It refers to this statement that appears earlier:
If you want to standardize the format in which you want to print dates across your application, using a hardcoded string is not
a good idea because it is prone to typos and misunderstandings. DateTimeFormatter provides a better way to create formatter objects through its static ofXXX methods.
So, instead of the ofPattern methods, focus more on the ofXXX(FormatStyle fs) methods.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by flex567 »

I think it would be great if there was an example of use of the format method
You can get a string representation of a date/time object using either the toString method or the
format(DateTimeFormatter dtf) method.
Just to not confuse it with DateTimeFormatter's format method.

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by admin »

sure, thanks for your suggestion.

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by Username987654 »

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy MMM dd");
System.out.println(dtf.format(LocalDate.now()));
System.out.println(dtf.format(LocalDateTime.now()));
The above code prints:
18 Apr 29
18 Apr 29
An interesting thing to note in the above output is that no exception is thrown at the third line.
The pattern that we used to create the DateTimeFormatter does not include any information for
time. Yet, when we used this formatter to format a LocalDateTime, it didn’t complain. It simply
ignored the time component of the LocalDateTime object.
What makes this situation (different and) not complain? Is this an isolated case? If not, is there a "rule of thumb" (or specific circumstances) identifying when using a formatter similarly doesn't complain?

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by Username987654 »

Username987654 wrote:
Sat Jul 06, 2019 6:53 pm
What makes this situation (different and) not complain? Is this an isolated case? If not, is there a "rule of thumb" (or specific circumstances) identifying when using a formatter similarly doesn't complain?
It does not complain because with the third line, we have both date and time information being passed in to be formatted. Thus, the object on which we are invoking the format is on the DateTimeFormatter object, as opposed to a LocalDate object. Correct?

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by admin »

That is correct.

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by raphaelzintec »

hello

this is not true

"""Using FormatStyle.LONG or FormatStyle.FULL for formatting a LocalDateTime or LocalTime
will throw an exception at run time because these forms expect time zone information in the time
component, which is not present in LocalDateTime or LocalTime. The exam does not expect you
to remember the formats in which these formatters print the date/time. You only need to know
how standardized formatters can be created.'""

You can use FormatStyle.LONG or FormatStyle.FULL on LocalDateTime, but indeed u cannot use it on LocalTime

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

Re: [HD Pg 346, Sec. 12.3.3 - converting-date-time-objects-to-strings]

Post by raphaelzintec »

sorry my bad its correct

Code: Select all

    public static void main(String[] args)  {
        DateTimeFormatter dtfDateOnly1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        DateTimeFormatter dtfDateOnly2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        DateTimeFormatter dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
        DateTimeFormatter dtf2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);

        LocalDate ld = LocalDate.now();
        System.out.println(dtfDateOnly1.format(ld));
        System.out.println(dtfDateOnly2.format(ld));

        LocalDateTime ldt = LocalDateTime.now();
        ZonedDateTime zdt = ZonedDateTime.now();
        OffsetDateTime odt = OffsetDateTime.now();

        /*FULL*/
        //System.out.println(dtf1.format(ldt)); DateTimeException
        System.out.println(dtf1.format(zdt));
        //System.out.println(dtf1.format(odt)); DateTimeException

        /*LONG*/
        //System.out.println(dtf2.format(ldt)); DateTimeException
        System.out.println(dtf2.format(zdt));
        //System.out.println(dtf2.format(odt)); DateTimeException

    }
        
So basically FULL & LONG work only with LocalDate or ZonedDateTime

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests