Also, please clarify the following sentence from the end of the one-before-last paragraph of the section.Just like the no-args parse method,
ThanksYou only need to know how standardized formatters can be created.
Moderator: admin
Also, please clarify the following sentence from the end of the one-before-last paragraph of the section.Just like the no-args parse method,
ThanksYou only need to know how standardized formatters can be created.
Yes, but it should really say something like just like the parse method with no formatter.
It refers to this statement that appears earlier:Also, please clarify the following sentence from the end of the one-before-last paragraph of the section.
ThanksYou only need to know how standardized formatters can be created.
So, instead of the ofPattern methods, focus more on the ofXXX(FormatStyle fs) methods.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.
Just to not confuse it with DateTimeFormatter's format method.You can get a string representation of a date/time object using either the toString method or the
format(DateTimeFormatter dtf) method.
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?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.
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?Username987654 wrote: ↑Sat Jul 06, 2019 6:53 pmWhat 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?
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
}
Users browsing this forum: Bing [Bot] and 7 guests