Java Reference
In-Depth Information
// Convert Thai Buddhist date to ISO date and vice versa
ThaiBuddhistDate thaiBuddhistNow2 = ThaiBuddhistDate.from(isoNow);
LocalDate isoNow2 = LocalDate.from(thaiBuddhistNow);
System.out.println("Thai Buddhist Current Date from ISO: " + thaiBuddhistNow2);
System.out.println("ISO Current Date from Thai Buddhist: " + isoNow2);
}
}
Thai Buddhist Current Date: ThaiBuddhist BE 2557-01-09
ISO Current Date: 2014-01-09
Thai Buddhist Current Date from ISO: ThaiBuddhist BE 2557-01-09
ISO Current Date from Thai Buddhist: 2014-01-09
Formatting Dates and Times
An object of the DateTimeFormatter class lets you format and parse a datetime object. By formatting, I mean
representing a datetime object in a user-defined textual form, for example, representing a LocalDate May 24, 2012 as
“05/24/2012.” Sometimes formatting is also referred as printing, because formatting features also let you print
(or output) a textual representation of a datetime object to an Appendable object such as a StringBuilder .
Parsing is the reverse of formatting. It lets you construct a datetime object from a textual representation of a
datetime. Creating a LocalDate object from the text “05/24/2012” to represent May 24, 2012 is an example of parsing.
Different ways of formatting and parsing datetimes exist. Learning how to format datetimes may be difficult,
if you do not learn it the right way. The most important point to keep in mind is that formatting and parsing are
always performed by an object of the DateTimeFormatter class. The difference lies in how you create that object.
The DateTimeFormatter class does not provide any public constructors. You must obtain its object indirectly. In the
beginning, the confusion lies in how to get its object. Use one of the following two methods of the DateTimeFormatter
class to format a date, time, or datetime:
String format(TemporalAccessor temporal)
void formatTo(TemporalAccessor temporal, Appendable appendable)
The format() method takes a date, time, or datetime object and returns a textual representation of the object
based on the rules of the formatter. The formatTo() method lets you write the textual representation of the object to
an Appendable , for example, a file, a StringBuilder , etc.
To format a datetime object, a formatter needs two pieces of information: a format pattern and a locale.
Sometimes one or both pieces of information are defaulted for you; sometimes, you provide them.
You can perform formatting in several ways. They all use, directly or indirectly, a DateTimeFormatter object.
Using pre-defined standard datetime formatters
Using the
format() method of the datetime classes
Using user-defined patterns
Using the
DateTimeFormatterBuilder class
Using Predefined Formatters
Predefined formatters are defined as constants in the DateTimeFormatter class. They are listed in in Table 12-5 . Most
of the formatters use ISO datetime formats; some formatters use a slightly modified version of ISO formats.
 
 
Search WWH ::




Custom Search