Java Reference
In-Depth Information
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
OffsetDateTime dateTimeInNewYork = OffsetDateTime.of(date, newYorkOffset);
Another advanced feature supported by the new Date and Time API is support for non-ISO
calendaring systems.
12.3.2. Using alternative calendar systems
The ISO-8601 calendar system is the de facto world civil calendar system. But four additional
calendar systems are provided in Java 8. Each of these calendar systems has a dedicated date
class: ThaiBuddhistDate, MinguoDate, JapaneseDate, and HijrahDate. All these classes together
with LocalDate implement the ChronoLocalDate interface intended to model a date in an
arbitrary chronology. You can create an instance of one of these classes out of a LocalDate. More
generally, you can create any other Temporal instance using their from static factory methods as
follows:
LocalDate date = LocalDate.of(2014, Month.MARCH, 18);
JapaneseDate japaneseDate = JapaneseDate.from(date);
Alternatively, you can explicitly create a calendar system for a specific Locale and create an
instance of a date for that Locale. In the new Date and Time API, the Chronology interface
models a calendar system, and you can obtain an instance of it using its ofLocale static factory
method:
Chronology japaneseChronology = Chronology.ofLocale(Locale.JAPAN);
ChronoLocalDate now = japaneseChronology.dateNow();
The designers of the Date and Time API advise using LocalDate instead of Chrono-LocalDate for
most cases; this is because a developer could make assumptions in their code that unfortunately
aren't true in a multicalendar system. Such assumptions might include that the value of a day or
month will never be higher than 31, that a year contains 12 months, or even that a year has a
fixed number of months. For these reasons, it's recommended to use LocalDate throughout your
application, including all storage, manipulation, and interpretation of business rules, whereas
you should employ Chrono-LocalDate only when you need to localize the input or output of your
program.
 
Search WWH ::




Custom Search