1 Copyright 2013 Oracle andor its affiliates All

  • Slides: 46
Download presentation
1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Converting to the New Date and Time API in JDK 8 Roger Riggs Oracle

Converting to the New Date and Time API in JDK 8 Roger Riggs Oracle Consulting Member of Technical Staff Sherman Shen Oracle Principal Member of Technical Staff Stephen Colebourne Open. Gamma Ltd Member of Technical Staff

The following is intended to outline our general product direction. It is intended for

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Program Agenda § Java. time Design Patterns § Calendar Types Comparison – java. time

Program Agenda § Java. time Design Patterns § Calendar Types Comparison – java. time vs java. util § Regional Calendars § Formatting and Localization § Interoperation with Joda. Time 4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Introduction to java. time § ISO Core Calendar – java. time – Local. Time,

Introduction to java. time § ISO Core Calendar – java. time – Local. Time, Local. Date. Time, Zoned. Date. Time, … – Clock, Instant, Duration, Period, Zone. Id, Month, Day. Of. Week, … § Parsing and Formatting – java. time. format – Date. Time. Format, Date. Time. Format. Builder, standard formats, patterns, styles, … § Regional Calendars – java. time. chrono – Chronology, Era, Chrono. Local. Date. Time, Chrono. Zoned. Date. Time, … – Japanese, Thai. Buddhist, Minguo, Hijrah calendars § Framework – java. time. temporal – Units, Fields, Adjusters, Temporal. Accessor, Temporal. Amount, Temporal. Query, … § Time. Zone – java. time. zone – 5 Zone. Rules, transitions, etc. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

ISO Calendar Types 6 Local. Date Local. Time Local. Date. Time Offset. Date. Time

ISO Calendar Types 6 Local. Date Local. Time Local. Date. Time Offset. Date. Time Zoned. Date. Time 2010 -12 -03 Year. Month. Day 2010 -12 -12 -03 Instant 2576458258. 266 seconds after 1970 -01 -01 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 11: 05: 30 2010 -12 -03 T 11: 05: 30+01: 00 Europe/Paris

API Design Comparison Java. time 7 vs. java. util. Calendar and Date § Fluent

API Design Comparison Java. time 7 vs. java. util. Calendar and Date § Fluent API § Not Fluent § Immutable instances § Mutable instances – clone needed § Thread safe § Not Thread safe § Strong types § Weakly typed calendars § Fit for purpose types § One size fits all API Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Getting Date Time Fields Zoned. Date. Time zdt =. . . ; int nanos

Getting Date Time Fields Zoned. Date. Time zdt =. . . ; int nanos = zdt. get. Nano(); int millis = zdt. get(MILLIS_OF_SECOND); int second = zdt. get. Second(); int minute = zdt. get. Minute(); int hour = zdt. get. Hour(); int day = zdt. get. Day. Of. Month(); Month month = zdt. get. Month(); int monthval = zdt. get. Month. Value(); int year = zdt. get. Year(); 8 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Gregorian. Calendar cal =. . . ; int int int millis = cal. get(Calendar. MILLISECOND); second = cal. get(Calendar. SECOND); minute = cal. get(Calendar. MINUTE); hour = cal. get(Calendar. HOUR); day = cal. get(Calendar. DAY_OF_MONTH); int monthval = cal. get(Calendar. MONTH) + 1; int year = cal. get(Calendar. YEAR);

Setting Date Time Fields 9 Zoned. Date. Time zdt =. . . ; Gregorian.

Setting Date Time Fields 9 Zoned. Date. Time zdt =. . . ; Gregorian. Calendar cal =. . . ; zdt Zdt zdt zdt zdt cal. set(Calendar. MILLISECOND, 1); cal. set(Calendar. MINUTE, 1); cal. set(Calendar. HOUR, 1); cal. set(Calendar. DAY_OF_MONTH, 1); cal. set(Calendar. MONTH, 1 - 1); cal. set(Calendar. YEAR, 1); = = = = zdt. with. Nano(1); zdt. with(MILLI_OF_SECOND, 1); zdt. with. Second(1); zdt. with. Minute(1); zdt. with. Hour(1); zdt. with. Day. Of. Month(1); zdt. with. Year(1); Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Date Time Field Math 10 Zoned. Date. Time zdt =. . . ; Gregorian.

Date Time Field Math 10 Zoned. Date. Time zdt =. . . ; Gregorian. Calendar cal =. . . ; zdt zdt = = = = zdt. plus. Nanos(1); zdt. plus(1, MILLIS); zdt. plus. Seconds(1); zdt. plus. Minutes(1); zdt. plus. Hours(1); zdt. plus. Days(1); zdt. plus. Months(1); zdt. plus. Years(1); // Modify the fields cal. add(Calendar. MILLISECOND, 1); cal. add(Calendar. MINUTE, 1); cal. add(Calendar. HOUR, 1); cal. add(Calendar. DAY_OF_MONTH, 1); cal. add(Calendar. YEAR, 1); zdt zdt = = = = zdt. minus. Nanos(1); zdt. minus(1, MILLIS); zdt. minus. Seconds(1); zdt. minus. Minutes(1); zdt. minus. Hours(1); zdt. minus. Days(1); zdt. minus. Months(1); zdt. minus. Years(1); cal. add(Calendar. MILLISECOND, -1); cal. add(Calendar. MINUTE, -1); cal. add(Calendar. HOUR, -1); cal. add(Calendar. DAY_OF_MONTH, -1); cal. add(Calendar. YEAR, -1); Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Comparison of Calendar Types Fit for purpose Date Time types Java. time ISO Calendar

Comparison of Calendar Types Fit for purpose Date Time types Java. time ISO Calendar Java. util Calendar Instant Date Local. Date, Calendar Local. Time, Local. Date. Time 11 Zoned. Date. Time Calendar Offset. Date. Time, Offset. Time, Calendar Zone. Id, Zone. Offset, Zone. Rules Time. Zone Week Starts on Monday (1. . 7) Week Starts on Sunday (1. . 7) enum MONDAY, TUESDAY, … SUNDAY int values SUNDAY, MONDAY, … SATURDAY 12 Months (1. . 12) 12 Months (0. . 11) enum JANUARY, FEBRUARY, …, DECEMBER int values JANUARY, FEBRUARY, … DECEMBER Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Design Patterns of Method Names Prefix 12 Method Type Use Creates an instance where

Design Patterns of Method Names Prefix 12 Method Type Use Creates an instance where the factory is primarily validating the input parameters, not converting them Converts the input parameter to an instance of the target class, which may lose information from the input of static factory from static factory date, date. Now static factory Creates a date from the arguments or from a clock or current time. parse static factory Parses the input string to produce an instance of the target class format instance Uses a formatter to format the values in the temporal object to a string get instance Returns a part of the state of the target object with instance plus, minus instance to instance Converts this object to another type at instance Combines this object with another is. Before, is. After, is. Equal Instance Compares this object with another on the timeline Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Returns a copy of the target object with one element changed; the immutable equivalent to set Returns a copy of the target object with an amount of time added or subtracted

Static Imports Improve Readability Local. Date date = Local. Date. of(2013, MONTH. JUNE, 30);

Static Imports Improve Readability Local. Date date = Local. Date. of(2013, MONTH. JUNE, 30); Month month = Month. DECEMBER; int mon. Index = month. get. Value(); // 1. . 12 assert(Month. DECEMBER. get. Value() == 12); 13 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Static Imports Improve Readability static import java. time. Month. JUNE; static import java. time.

Static Imports Improve Readability static import java. time. Month. JUNE; static import java. time. Month. DECEMBER; Local. Date date = Local. Date. of(2013, MONTH. JUNE, 30); Month month = Month. DECEMBER; int mon. Index = month. get. Value(); // 1. . 12 assert(Month. DECEMBER. get. Value() == 12); 14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Static Imports Improve Readability static import java. time. Month. JUNE; static import java. time.

Static Imports Improve Readability static import java. time. Month. JUNE; static import java. time. Month. DECEMBER; Local. Date date = Local. Date. of(2013, JUNE, 30); Month month = DECEMBER; int mon. Index = month. get. Value(); assert(DECEMBER. get. Value() == 12); // 1. . 12 15 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Calendar Neutral API 16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Calendar Neutral API 16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Calendar Neutral API § The core java. time APIs are designed to avoid common

Calendar Neutral API § The core java. time APIs are designed to avoid common errors § For other Calendars common assumptions may be invalid – Don't assume the number of months of year – Use the API to do all calendar arithmetic. Plus/minus days, months, years – Do not assume roll-over at particular numbers of days-per-month or months-per-year – Don't assume the week starts on Monday (ISO), use Week. Fields. – Don't assume the month numbers are bound to specific months, use Date. Time. Formatter to get names 17 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Chronology Based Calendars Access is field based, modification by units import static java. time.

Chronology Based Calendars Access is field based, modification by units import static java. time. temporal. Chrono. Field. *; import static java. time. temporal. Chrono. Unit. *; Chrono. Local. Date. Time datetime = …; int int date month year era = = datetime. get(DAY_OF_MONTH); datetime. get(MONTH_OF_YEAR); datetime. get(YEAR_OF_ERA); datetime. get(ERA); int hour = datetime. get(HOUR_OF_DAY); int minute = datetime. get(MINUTE_OF_HOUR); int second = datetime. get(SECOND_OF_MINUTE); datetime datetime 18 = = = = datetime. plus(1, datetime. plus(1, Copyright © 2013, Oracle and/or its affiliates. All rights reserved. NANOS); SECONDS); MINUTES); HOURS); DAYS); MONTHS); YEARS);

Calendar Neutral API Mapping Accessed by Fields and Units 19 Java. util. Calendar Java.

Calendar Neutral API Mapping Accessed by Fields and Units 19 Java. util. Calendar Java. time. temporal. Chrono. Field Java. time. temporal. Chrono. Units Calendar. DAY_OF_MONTH Chrono. Field. DAY_OF_MONTH Chrono. Unit. DAYS Calendar. MONTH Chrono. Field. MONTH_OF_YEAR Chrono. Unit. MONTHS Calendar. YEAR Chrono. Field. YEAR Chrono. Unit. YEARS Calendar. HOUR Chrono. Field. HOUR_OF_DAY Chrono. Unit. HOURS Calendar. MINUTE Chrono. Field. MINUTE_OF_HOUR Chrono. Unit. MINUTES Calendar. SECOND Chrono. Field. SECOND_OF_DAY Chrono. Unit. SECONDS Calendar. MILLISECOND Chrono. Field. MILLIS_OF_SECOND Chrono. Unit. NANOS Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Regional Calendars Listing, lookup, creating dates Set<Chronology> chronologies = Chronology. get. Available. Chronologies(); for

Regional Calendars Listing, lookup, creating dates Set<Chronology> chronologies = Chronology. get. Available. Chronologies(); for (Chronology chrono : chronologies) { System. out. printf(" %s (%s)%n", chrono. get. Id(), chrono. get. Calendar. Type()); } // Lookup by name or CLDR type Chronology chrono = Chronology. of("Hijrah"); chrono = Chronology. of("islamic-umalqura"); // Select calendar based on the locale based on BCP 47 language tags Locale locale = Locale. for. Language. Tag("ja-JP-u-ca-japanese"); chrono = Chronology. of. Locale(locale); // Create date based on proleptic year, month, day Chrono. Local. Date date = chrono. date(2013, 9, 22); 20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Calendar Neutral Dates import static java. time. temporal. Chrono. Field. *; import static java.

Calendar Neutral Dates import static java. time. temporal. Chrono. Field. *; import static java. time. temporal. Chrono. Unit. *; import static java. util. Calendar. *; Locale locale = …; Chronology chrono = Chronology. of. Locale(locale); Calendar cal = Calendar. get. Instance(locale); Chrono. Local. Date date = chrono. date. Now(); int int Era day month year era = = date. get(DAY_OF_MONTH); date. get(MONTH_OF_YEAR); date. get(YEAR_OF_ERA); date. get(ERA) Chrono. Local. Date nextmonth = date. with(DAY_OF_MONTH, 1). plus(1, MONTHS); 21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. int int day month year era = = cal. get(DAY_OF_MONTH); cal. get(MONTH) + 1; cal. get(YEAR); cal. get(ERA); Calendar next. Month = cal. clone(); next. Month. set(DAY_OF_MONTH, 1); next. Month. add(MONTH, 1);

Calendar Neutral Dates and Times Chronology chrono = …; Chrono. Local. Date date =

Calendar Neutral Dates and Times Chronology chrono = …; Chrono. Local. Date date = chrono. date. Now(); // Calendar neutral dates and times combinations. Chrono. Local. Date. Time<? > cldt = date. at. Time(Local. Time. NOON); Chrono. Local. Date d = cldt. to. Local. Date(); Local. Time t = cldt. to. Local. Time(); Zone. Id zone = Zone. Id. of(“Europe/Paris"); Chrono. Zoned. Date. Time<? > czdt = cldt. at. Zone(zone); czdt = czdt. with(Local. Time. MIDNIGHT); Output: 2013 -10 -01 T 00: 00+02: 00[Europe/Paris] 22 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Japanese Date, Eras and Chronology static import java. time. chrono. Japanese. Era. HEISEI; Locale

Japanese Date, Eras and Chronology static import java. time. chrono. Japanese. Era. HEISEI; Locale locale = Locale. for. Language. Tag("ja-JP-u-ca-japanese"); Zone. Id zone = Zone. Id. of("Asia/Tokyo"); Calendar jcal = Calendar. get. Instance(locale); jcal. set. Time. Zone(Time. Zone. get. Time. Zone(zone)); jcal. set(Calendar. YEAR, 25); Japanese. Date jdate = jcal. set(Calendar. MONTH, 9 - 1); Japanese. Date. of(HEISEI, 25, 9, 22); jcal. set(Calendar. DAY_OF_MONTH, 22); Chrono. Local. Date. Time<Japanese. Date> jldt = jdate. at. Time(Local. Time. now()); Chrono. Zoned. Date. Time<Japanese. Date> jzdt = jldt. at. Zone(zone); czdt. format(Date. Time. Formatter. of. Pattern("GGGGyy-mm-dd"). with. Locale(locale)) jcal. add(Calendar. DAY_OF_MONTH, 2); Simple. Date. Format sdf = (Simple. Date. Format)Date. Format. get. Date. Instance(Simple. Date. Format. SHORT, locale); sdf. apply. Localized. Pattern("GGGGyy-mm-dd"); sdf. format(jcal. get. Time()) Output: 平成 25 -09 -24 jzdt = jzdt. plus(2, Chrono. Unit. DAYS); 23 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Hijrah Islamic Calendar Zone. Id zone = Zone. Id. of("Asia/Riyadh"); Hijrah. Date hdate =

Hijrah Islamic Calendar Zone. Id zone = Zone. Id. of("Asia/Riyadh"); Hijrah. Date hdate = Hijrah. Date. now(); date = date. plus(1, Chrono. Unit. DAYS); Chrono. Local. Date. Time<Hijrah. Date> cldt = hdate. at. Time(Local. Time. NOON); Chrono. Zoned. Date. Time<Hijrah. Date> hzdt = cldt. at. Zone(zone); hzdt = hzdt. plus(2, Chrono. Unit. DAYS); Hijrah. Date hdate 2 = hzdt. to. Local. Date(); Local. Time htime 2 = hzdt. to. Local. Time(); czdt. format(Date. Time. Formatter. of. Localized. Date(Format. Style. FULL). with. Locale(locale)) Output: 2013 , ﺳﺒﺘﻤﺒﺮ 13 24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Formatting and Localization 25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Formatting and Localization 25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Predefined Date Time Formats Zoned. Date. Time now = Zoned. Date. Time. now(); String

Predefined Date Time Formats Zoned. Date. Time now = Zoned. Date. Time. now(); String to. String = now. to. String(); // 2013 -08 -06 T 18: 12: 17. 423 -04: 00[America/New_York] 26 Formatter Example of. Localized. Date. Time(date. Style, '3 Jun 2008 11: 05' time. Style) BASIC_ISO_DATE '20111203' Date. Time. Formatter fmt = Date. Time. Formatter. ISO_ZONED_DATE_TIME; ISO_LOCAL_DATE '2011 -12 -03' ISO_OFFSET_DATE '2011 -12 -03+01: 00' String s = now. format(fmt); // 2013 -08 -06 T 18: 12: 17. 423 -04: 00[America/New_York] ISO_DATE '2011 -12 -03+01: 00'; '2011 -12 -03' ISO_LOCAL_TIME '10: 15: 30' ISO_OFFSET_TIME '10: 15: 30+01: 00' ISO_TIME '10: 15: 30+01: 00'; '10: 15: 30' ISO_LOCAL_DATE_TIME '2011 -12 -03 T 10: 15: 30' ISO_OFFSET_DATE_TIME 2011 -12 -03 T 10: 15: 30+01: 00' ISO_ZONED_DATE_TIME '2011 -1203 T 10: 15: 30+01: 00[Europe/Paris]' ISO_INSTANT '2011 -12 -03 T 10: 15: 30 Z' RFC_1123_DATE_TIME 'Tue, 3 Jun 2008 11: 05: 30 GMT' Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

java. util. Formatter supports java. time types String. format, Print. Stream. format, … Locale

java. util. Formatter supports java. time types String. format, Print. Stream. format, … Locale locale = Locale. get. Default(); Formatter fmt = new Formatter(System. out, locale); Date date = new Date(); Calendar cal = Calendar. get. Instance(); Local. Date ld = Local. Date. now(); Local. Time lt = Local. Time. now(); Local. Date. Time ldt = Local. Date. Time. now(); Zoned. Date. Time zdt = Zoned. Date. Time. now(); fmt. format("date: fmt. format(" cal: fmt. format(" Local. Date: fmt. format(" Local. Time: fmt. format("Local. Date. Time: ldt); fmt. format("Zoned. Date. Time: %tc%n", date); %tc%n", cal); %1$ta %1$tb %1$td %1$t. Y%n", ld); %t. T %n", lt); %1$ta %1$tb %1$td %1$t. T %1$t. Y%n", date: cal: Local. Date: Local. Time: Local. Date. Time: Fri Aug 09 15: 16: 33 EDT 2013 15: 16: 33 2013 %tc%n", zdt); Zoned. Date. Time: Fri Aug 09 15: 16: 33 EDT 2013 28 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Date. Time. Formatter Predefined and Customizable Patterns import static java. time. format. Date. Time.

Date. Time. Formatter Predefined and Customizable Patterns import static java. time. format. Date. Time. Formatter. *; Zoned. Date. Time now = Zoned. Date. Time. now(); 29 now. to. String(); 2013 -08 -09 T 15: 20: 40. 167 -04: 00[America/New_York] now. format(RFC_1123_DATE_TIME) now. format(ISO_DATE_TIME) now. format(of. Localized. Date. Time(Format. Style. FULL)) Fri, 9 Aug 2013 15: 20: 40 -0400 2013 -08 -09 T 15: 20: 40. 167 -04: 00[America/New_York] Friday, August 9, 2013 3: 20: 40 PM EDT now. format(of. Pattern("z zz zzzz VV")) EDT EDT Eastern Daylight Time America/New_York now. format(of. Pattern("y'-W'w-e")) now. format(of. Pattern("h: ss a")) now. format(of. Pattern("'Q'q y")) 2013 -W 37 -3 4: 02 PM Q 3 2013 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Locale based Date and Time formats Use Date. Time. Formatter instead of Simple. Date.

Locale based Date and Time formats Use Date. Time. Formatter instead of Simple. Date. Format Locale locale = …; Gregorian. Calendar cal = (Gregorian. Calendar) Calendar. get. Instance(); Date. Time. Formatter dtf = Date. Time. Formatter. of. Localized. Date. Time(Format. Style. MEDIUM, Format. Style. MEDIUM). with. Locale(locale); 30 Locale locale = …; Calendar cal = Calendar. get. Instance(); Simple. Date. Format sdf = (Simple. Date. Format)Date. Format. get. Date. Time. Instance(Simple. Date. Format. MEDIUM, locale); Zoned. Date. Time zdt = cal. to. Zoned. Date. Time(); dtf. format(zdt); Date date = cal. get. Time(); sdf. format(date); Output: Aug 9, 2013 3: 26: 10 PM Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Daylight Savings Time Change Example Finding the next change Zoned. Date. Time zdt =

Daylight Savings Time Change Example Finding the next change Zoned. Date. Time zdt = Zone. Date. Time. now(); Zone. Id zid = zdt. get. Zone(); Zone. Rules rules = zid. get. Rules(); Zone. Offset. Transition transition = rules. next. Transition(zdt. to. Instant()); Duration duration = transition. get. Duration(); Zoned. Date. Time when = transition. get. Date. Time. Before(). at. Zone(zid); String fmt. String = duration. is. Negative() ? " In the Fall on %s, fallback by %s%n" : " In the Spring on %s, spring forward by %s%n"; System. out. printf(fmt. String, when. format(Date. Time. Formatter. of. Localized. Date. Time(Format. Style. FULL)), Local. Time. of. Second. Of. Day(duration. abs(). get. Seconds())); Output: In the Fall on Sunday, November 3, 2013 2: 00 AM EST, fallback by 01: 00 31 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Date. Time. Formatter Builder Customized parsing and formatting § Factory to build a template

Date. Time. Formatter Builder Customized parsing and formatting § Factory to build a template for a sequence of fields to be parsed or formatted § Literals, for example “/”, “-”, “: ”, or any string § Numeric values with control of width, sign, leading zeros § Text indexed by locale and style – short, narrow, full, standalone § Patterns indexed by Locale - date, time, date-time § Fraction control, with control of width and decimal points § Field padding to width and character § Zone. Id, Zone. Offset, Chronology, Instant specialized fields 32 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Date. Time. Formatter Builder II § Case sensitive vs. in-sensitive parsing § Nested and

Date. Time. Formatter Builder II § Case sensitive vs. in-sensitive parsing § Nested and Optional fields § Default values if not present in input § Strict vs. Lenient parsing mode § Concatenation of Formatters 33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Formatter Builder Example Parse time with optional minutes and seconds Date. Time. Formatter fmt

Formatter Builder Example Parse time with optional minutes and seconds Date. Time. Formatter fmt = new Date. Time. Formatter. Builder(). append. Value(HOUR_OF_DAY, 1, 2, Sign. Style. NEVER). optional. Start(). append. Literal(": "). append. Value(MINUTE_OF_HOUR, 2). optional. Start(). append. Literal(": "). append. Value(SECOND_OF_MINUTE, 2). optional. End(). parse. Defaulting(MINUTE_OF_HOUR, 1). parse. Defaulting(SECOND_OF_MINUTE, 0). to. Formatter(); Local. Time date = Local. Time. parse(s, fmt); System. out. printf(" Parsed %10 s --- %7 s%n", s, date. format(fmt)); Output: Parsed Parsed 34 9 09: 05 09: 30: 59 12: 10 23: 00 → → → 9: 01: 00 9: 05: 00 9: 30: 59 12: 10: 00 23: 00 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Date. Picker Control in Java. FX § Settable Chronology § Supports Regional Formatting via

Date. Picker Control in Java. FX § Settable Chronology § Supports Regional Formatting via Locale § Customizable Cell Factory import javafx. scene. control. Date. Picker; date. Picker = new Date. Picker(); date. Picker. set. On. Action(new Event. Handler<Action. Event>() { @Override public void handle(Action. Event t) { Local. Date iso. Date = date. Picker. get. Value(); println("Selected date: “ + iso. Date); } }); Output: Selected date: 2013 -09 -23 35 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Temporal Adjusters 36 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Temporal Adjusters 36 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Using Temporal Adjusters A function to apply to a date time Local. Date date

Using Temporal Adjusters A function to apply to a date time Local. Date date = …; // Advance to Last Day of the Month Local. Date end. Of. Month = date. with(Temporal. Adjuster. last. Day. Of. Month()); // Advance to last Friday of the Month Local. Date last. Friday = date. with(Temporal. Adjuster. last. In. Month(Day. Of. Week. FRIDAY)); Local. Date. Time datetime = …; // Return the second Monday of the month, retaining the time Local. Date. Time second. Monday = datetime. with(Temporal. Adjuster. day. Of. Week. In. Month(2, Day. Of. Week. MONDAY)); // Go back to the previous Wednesday, retaining the time Local. Date. Time previous. Wed = datetime. with(Temporal. Adjuster. previous(Day. Of. Week. WEDNESDAY)); 37 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Writing a Temporal Adjuster Payday Function as Temporal. Adjuster // Paydays are the 15

Writing a Temporal Adjuster Payday Function as Temporal. Adjuster // Paydays are the 15 th and the last day of the month or // the previous Friday if it lands on a weekend. static Temporal next. Payday(Temporal curr. Date) { // Advance at least 1 day and skip Saturday and Sunday int past. Friday = curr. Date. get(Chrono. Field. DAY_OF_WEEK) - Day. Of. Week. FRIDAY. get. Value(); int extra. Days = (past. Friday < 0) ? 1 : 2 - past. Friday + 1; Temporal date = curr. Date. plus(extra. Days, Chrono. Unit. DAYS); // Payday is the 15 th or the last day of the month int day = date. get(Chrono. Field. DAY_OF_MONTH); day = (day <= 15) ? 15 : (int)date. range(Chrono. Field. DAY_OF_MONTH). get. Maximum(); Temporal payday = date. with(Chrono. Field. DAY_OF_MONTH, day); // If it falls on a Saturday or Sunday, backup to the previous Friday past. Friday = payday. get(Chrono. Field. DAY_OF_WEEK) - Day. Of. Week. FRIDAY. get. Value(); if (past. Friday > 0) { payday = payday. minus(past. Friday, Chrono. Unit. DAYS); } return payday; } 38 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Compare with Gregorian Pay. Day Function static Gregorian. Calendar next. Payday(Gregorian. Calendar date) {

Compare with Gregorian Pay. Day Function static Gregorian. Calendar next. Payday(Gregorian. Calendar date) { // Advance to the first non-weekend Gregorian. Calendar payday = (Gregorian. Calendar)date. clone(); payday. add(Gregorian. Calendar. DAY_OF_MONTH, 1); int dow = payday. get(Gregorian. Calendar. DAY_OF_WEEK); if (dow == Gregorian. Calendar. SUNDAY) { payday. add(Gregorian. Calendar. DAY_OF_MONTH, 1); } else if (dow == Gregorian. Calendar. SATURDAY) { payday. add(Gregorian. Calendar. DAY_OF_MONTH, 2); } int day = payday. get(Gregorian. Calendar. DAY_OF_MONTH); day = (day <= 15) ? 15 : payday. get. Actual. Maximum(Gregorian. Calendar. DAY_OF_MONTH); payday. set(Gregorian. Calendar. DAY_OF_MONTH, day); // If it falls on a Saturday or Sunday, backup to the previous Friday dow = payday. get(Gregorian. Calendar. DAY_OF_WEEK); if (dow == Gregorian. Calendar. SUNDAY) { payday. add(Gregorian. Calendar. DAY_OF_MONTH, -2); } else if (dow == Gregorian. Calendar. SATURDAY) { payday. add(Gregorian. Calendar. DAY_OF_MONTH, -1); } return payday; } 39 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Joda-Time Comparison 40 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Joda-Time Comparison 40 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Similar Naming and Design § Most methods have similar names § Mapping of class

Similar Naming and Design § Most methods have similar names § Mapping of class names – Local. Date, Local. Time, Local. Date. Time are the same – Joda-Time Date. Time is java. time. Zoned. Date. Time – Joda-Time Date. Time. Zone maps to java. time. Zone. Id/Zone. Rules/Zone. Offset § Period and Duration – Joda-Time Period is everything from years to seconds – Java. time. Period is simpler, only ISO date-based – Joda-Time Duration is same as java. time. Duration § Formatters immutable in both Joda-Time and 310 – formatter builder very similar 41 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Joda-time vs. JSR 310 § 310 is more open and flexible – Adjusters and

Joda-time vs. JSR 310 § 310 is more open and flexible – Adjusters and queries are new – Ability to define own fields and units is essentially new – Adding own calendar systems is simple rather than very hard § Hope is to provide a version of Joda-Time that implements the Temporal interface 42 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Summary New Improved Date Time API § Fluent, Immutable, Thread Safe, Easy to use

Summary New Improved Date Time API § Fluent, Immutable, Thread Safe, Easy to use § Strong typing with fit for purpose types § Easy to use formatting and parsing § Extensible with Units, Fields, and Chronologies § Interoperable with java. util. Calendar § Supports Regional Calendars § The essential ISO Calendar for global business 43 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Questions… Answers… 44 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Questions… Answers… 44 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Resources § Java Date Time Tutorial – http: //docs. oracle. com/javase/tutorial/ § Early Access

Resources § Java Date Time Tutorial – http: //docs. oracle. com/javase/tutorial/ § Early Access JDK 8 Javadoc – http: //download. java. net/jdk 8/docs § Developer Preview Download – https: //jdk 8. java. net/download. html 45 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

The preceding is intended to outline our general product direction. It is intended for

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 46 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

47 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

47 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.