Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Date and Time API 2

Introduction to Date and Time API 2

HASUNUMA Kenji

November 09, 2013
Tweet

More Decks by HASUNUMA Kenji

Other Decks in Programming

Transcript

  1. Introduction to  Date and Time API II HASUNUMA Kenji

    GlassFish Community [email protected] Twitter: @khasunuma November 9, 2013 #ccc_r21
  2. java.util.Date ANSI/ISO Cͷtime_tͱػೳతʹಉ౳ ݱࡏ͸೔෇ɾ࣌ࠁͷදݱͷΈʹప͢Δ ˞java.sql.Date౳ͷαϒΫϥε java.util.Calendar JavaͷࠃࡍԽରԠ(JDK 1.1)Ͱಋೖ ೔෇ɾ࣌ࠁͷ࡞੒ɾฤूʹ༻͍Δ ˞GregorianCalendar౳ͷαϒΫϥε

    java.time.* (JSR 310) JDK8~ɺDate/Calendar୅ସ͕౰ॳͷ໨ඪ ೔෇ɾ࣌ࠁΛ૯߹తʹѻ͏ϑϨʔϜϫʔΫ ˞ن໛͸JDK8ͷ৽APIͰ΋࠷େڃ ˞ͦͷଞɺXML DOM APIͷXMLGregorianCalendar͕͋Δ
  3. java.util.Dateͷ՝୊ w ϑΟʔϧυૢ࡞͕໘౗ w ೥ϑΟʔϧυ+1900͕࣮ࡍͷ೥ w ݄͕͔Β࢝·Δ(1݄ˠ0ʜ12݄ˠ11) w ϑΟʔϧυͷ௚઀ૢ࡞͕ඇਪ঑(CalendarΛ࢖͏) w

    ೔෇෦ͱ࣌ࠁ෦͕ࠞࡏ͍ͯ͠Δ w ೔෇ԋࢉ͕ශऑɺϑΥʔϚοτػೳ΋࢖͍উखʹ೉ w +%,Ҏ߱ɺҰ෦ྫ֎Λআ͖ϝϯςφϯεͳ͠
  4. JSR 310 : Date and Time API wDateɺCalendarɺDateFormat౳Λஔ͖ ׵͑Δ͜ͱ͕໨త wISO

    8601ܗࣜͷ೔෇ɾ࣌ࠁදݱ wImmutable͔ͭεϨουηʔϑͳAPI wઃܭࢥ૝͸Joda-Timeʹࠅࣅ
  5. java.util.Date JSR 310 ΦϒδΣΫτ Mutable Immutable ਫ਼౓ ϛϦඵ φϊඵ ϑΟʔϧυૢ࡞

    Calendarܦ༝ ௚઀αϙʔτ λΠϜκʔϯ αϙʔτ αϙʔτ toString໭Γ஋ Unixܗࣜ ISO 8601ܗࣜ ೔෇ͱ࣌ࠁͷ෼཭ ෆՄ Մೳ ೔෇ԋࢉ ൺֱͷΈ ༷ʑͳ೔෇ԋࢉ ؔ࿈Ϋϥε਺ ਺ݸ ͨ͘͞Μ
  6. ࢀߟ: ISO 8601ܗࣜ w ೔෇yyyy-MM-dd • 2013-11-09 w ࣌ࠁhh:mm:ss.SSSZ •

    04:05:00.000Z w ೔࣌yyyy-MM-dd’T’HH:mm:ss.SSSZ • 2013-11-09T04:05:00.000Z
  7. ओͳDateTimeͷ࣮૷ • LocalDate, LocalTime, LocalDateTime • OffsetTime, OffsetDateTime • ZonedDateTime

    • ෆ଍৘ใͷ௥Ճ·ͨ͸༨৒৘ใͷ੾Γ ࣺͯʹΑΓ૬ޓʹม׵Մ
  8. %BUF 5JNF %BUF5JNF -PDBM ೥݄೔ ! ࣌ࠩ৘ใͳ͠ ! ࣌෼ඵະຬ ࣌ࠩ৘ใͳ͠

    ೥݄೔ ࣌෼ඵະຬ ࣌ࠩ৘ใͳ͠ 0⒎TFU /" ! ࣌෼ඵະຬ 65$͔Βͷ࣌ࠩ ೥݄೔ ࣌෼ඵະຬ 65$͔Βͷ࣌ࠩ ;POFE /" /" ೥݄೔ ࣌෼ඵະຬ λΠϜκʔϯ
  9. Chronology : ྐྵ ΠϯλϑΣʔε Chronology ྐྵͦͷ΋ͷΛද͠ɺ֤छม׵ ϝιουΛఏڙ ChronoLocalDate ྐྵͷ৘ใΛؚΉLocalDate ChronoLocalDateTime


    <D extends ChronoLocalDate> ChronoLocalDateʹ
 ࣌ࠁΛ෇Ճ ChronoZonedDateTime
 <D extends ChronoLocalDate> ChronoLocalDateʹ
 ࣌ࠁͱλΠϜκʔϯΛ෇Ճ Era ྐྵͷلݩ ։࢝೥ Λද͢ ˞࿨ྐྵ͸։࢝ɾऴྃ೥݄೔
  10. Chronologyͷ࣮૷Ϋϥε IsoChronology/-Era LocalDate ISO 8601 ඪ४ JapaneseChronology/-Era JapaneseDate ࿨ྐྵ ೔ຊ

    ThaiBuddistChronology/-Era ThaiBuddistDate ෹ྐྵ λΠ MinguoChronology/-Era MinguoDate ຽࠃلݩ ୆࿷ HijirahChronology/-Era HijirahDate ΠεϥϜྐྵ
  11. JSR 310ͷϑΥʔϚοτػೳ w LocalDate౳ͷtoStringϝιου͸ɺISO 8601ܗࣜ ͰϑΥʔϚοτͨ͠΋ͷΛฦ͢ w ඪ४Ҏ֎ͷϑΥʔϚοτˠDateTimeFormatter
 ˞DateTimeFormatterʹ͸࢖༻ස౓ͷߴ͍ϑΥʔϚοτ͕ఆٛࡁΈ w

    ϑΥʔϚοτΛ৽͘͠࡞੒͢Δ৔߹͸ɺ DateTimeFomatterBuilderͷ࢖༻Λਪ঑
 ˞ࠃ಺Ͱ͸yyyy/MM/ddܗ͕ࣜීٴ͍ͯ͠ΔͨΊɺԤถͱൺֱͯ͠ DateTimeFormatterBuilderͷ࢖༻ස౓͸ߴ͍ͱࢥΘΕΔ w java.text.DateFormat͸JSR 310Ͱ͸࢖༻Ͱ͖ͳ͍
 ˞ٯʹDateTimeFormatter͸java.text.DateFormatʹม׵Մೳ
  12. JSR 310㱻Date/Calendarม׵ ॏཁ : JSR 310͸Date/Calendarͱͷ௚઀ม׵Λ αϙʔτ͠ͳ͍ • Date͸InstantΛհͯ͠JSR 310ͱ૬ޓม׵͕Մೳ


    ۩ମతʹ͸Date.from() / Date.toInstant()Λ༻͍Δ • Calendar͸Calendar.toInstant()ͰInstantʹม׵Մೳɺ ͨͩͦ͠ͷٯ͸Ͱ͖ͳ͍ • Instant.toEpochMilli() / Instant.ofEpochMilli() Ͱ InstantΛlong஋ʹม׵͢Δํ๏΋͋Γ
  13. ThreeTen backport w JSR 310͔ΒJDK8ґଘͷ෦෼ΛऔΓআ͖ɺJDK7Ͱ ར༻Մೳʹͨ͠ɺJSR 310ϥΠΫͳϥΠϒϥϦ
 ύοέʔδ໊͸java.timeͰ͸ͳ͘org.threeten.bp  w

    Java SE 8ϦϦʔεલʹJSR 310Λ༧श͢ΔͨΊͷ ڭࡐͱͯ͠࠷ద w Apache License 2.0ͷOSSͱͯ͠഑෍
 https://github.com/ThreeTen/threetenbp
  14. // date: 1992೥10݄8೔ ͏Δ͏೥  // ˠ leap = true

    LocalDate date = LocalDate.of(1992, 10, 8); boolean leap = date.isLeapYear();
  15. // ࠓ೔ͷ೔෇ ࿨ྐྵ ˠjdate // ThreeTen BackportͰ΋݁Ռࣗମ͸ಉ͡ JapaneseDate jdate =


    JapaneseDate.now(); System.out.println(jdate.toString); H25-11-09 ग़ྗ݁Ռ
  16. Ԡ༻ : JSR 310ΛJAXBʹରԠͤͯ͞ΈΔ public class LocalDateXmlAdapter extends! XmlAdapter<XMLGregorianCalendar, LocalDate>

    {! ! @Override! public LocalDate unmarshal(XMLGregorianCalendar value) throws Exception {! int timezone = value.getTimezone();! ZoneOffset offset = ZoneOffset.ofTotalSeconds(timezone * 60);! return OffsetDateTime.of(value.getYear(), value.getMonth(),! value.getDay(), 0, 0, 0, 0, offset).toLocalDate();! }! ! @Override! public XMLGregorianCalendar marshal(LocalDate value) throws Exception {! ZoneOffset offset = OffsetDateTime.now().getOffset();! int timezone = offset.getTotalSeconds() / 60;! return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(! value.getYear(), value.getMonthValue(), value.getDayOfMonth(),! timezone);! }! ! }