Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Date and Time API
Search
HASUNUMA Kenji
November 10, 2012
Programming
0
29
Introduction to Date and Time API
HASUNUMA Kenji
November 10, 2012
Tweet
Share
More Decks by HASUNUMA Kenji
See All by HASUNUMA Kenji
Jakarta EE: The First Parts
khasunuma
0
46
Life of our small product
khasunuma
0
34
How to adapt MicroProfile API for generic Web applications
khasunuma
0
31
Overviewing Admin Console
khasunuma
0
30
Introduction to MicroProfile Metrics
khasunuma
0
52
Basic method for Java EE Web Profile
khasunuma
0
28
Introduction to JCA and MDB
khasunuma
0
72
Collections Framework Begineers Guide 2
khasunuma
0
64
JLS myths ~ if-then-else statement ~
khasunuma
0
33
Other Decks in Programming
See All in Programming
FlutterKaigi 2025 システム裏側
yumnumm
0
1.2k
最新のDirectX12で使えるレイトレ周りの機能追加について
projectasura
0
310
[SF Ruby Conf 2025] Rails X
palkan
0
380
All(?) About Point Sets
hole
0
230
大体よく分かるscala.collection.immutable.HashMap ~ Compressed Hash-Array Mapped Prefix-tree (CHAMP) ~
matsu_chara
1
180
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
230
生成AIを活用したリファクタリング実践 ~コードスメルをなくすためのアプローチ
raedion
0
160
TypeScriptで設計する 堅牢さとUXを両立した非同期ワークフローの実現
moeka__c
5
2.7k
手軽に積ん読を増やすには?/読みたい本と付き合うには?
o0h
PRO
1
120
チーム開発の “地ならし"
konifar
8
6.4k
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
2
2.4k
モダンJSフレームワークのビルドプロセス 〜なぜReactは503行、Svelteは12行なのか〜
fuuki12
0
140
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
697
190k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
It's Worth the Effort
3n
187
29k
A designer walks into a library…
pauljervisheath
210
24k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Thoughts on Productivity
jonyablonski
73
4.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1k
Site-Speed That Sticks
csswizardry
13
970
Practical Orchestrator
shlominoach
190
11k
Transcript
Introduction to Date and Time API HASUNUMA Kenji Vice
president, GlassFish Users Group Japan
[email protected]
Twitter: @btnrouge November 10, 2012
Javaʹ͓͚Δɾ࣌ࠁͷѻ͍ JDK 1.1 Ҏ߱ɺຊ࣭తʹมԽͳ͠
java.util.Date (JDK 1.0) wANSI/ISO Cͱػೳతʹಉ wUTCPSϩʔΧϧλΠϜκʔϯ w݄࣌ඵΛऔಘɾઃఆՄ wͱ࣌ࠁΛಉ͡ΫϥεͰද͢
java.util.Date (JDK 1.1) wࠃࡍԽରԠʜ࣮ΛCalendarϕʔεʹஔ ͖͑ w݄࣌ඵΛૢ࡞͢Δϝιου ͯ͢ඇਪˠCalendarͰସ wૢ࡞CalendarʹҠߦɺ%BUF ࣌Λࢦ͚ࣔͩ͢͠
java.util.Dateͷ՝ wϑΟʔϧυૢ࡞͕໘ w ϑΟʔϧυ ͕࣮ࡍͷ w ݄͕0͔Β࢝·Δ ݄ˠɺ݄ˠ w෦ͱ࣌ࠁ෦͕ࠞࡏ͍ͯ͠Δ
wԋࢉͱϑΥʔϚοτػೳ͕ශऑ
JSR 310 Date and Time API
Michael Nascimento Santos Stephen Colebourne Roger Riggs
JSR 310 : Date and Time API wDateɺCalendarɺDateFormatΛஔ͖ ͑Δ͜ͱ͕త wISO
8601ܗࣜͷɾ࣌ࠁදݱ wImmutable͔ͭεϨουηʔϑͳAPI wઃܭࢥJoda-Timeʹࠅࣅ
Joda-Time JSR 310
Second System Syndrome
More simply
JSR 310 : Date and Time API 20072݄JSR 310ঝೝɺEGൃ 20085݄JavaOne
2008Ͱൃද 20103݄Early Draft Review (1ճ) 201210݄Early Draft Review (2ճ) 201310݄ Java SE 8ϦϦʔε
java.util.Date JSR 310 ΦϒδΣΫτ Mutable Immutable ਫ਼ ϛϦඵ φϊඵ ϑΟʔϧυR/W
Calendarܦ༝ αϙʔτ λΠϜκʔϯ αϙʔτ αϙʔτ toStringΓ Unixܗࣜ ISO 8601ܗࣜ ͱ࣌ࠁͷ ෆՄ Մೳ ԋࢉ ൺֱͷΈ ༷ʑͳԋࢉ ؔ࿈Ϋϥε ݸ ͨ͘͞Μ
ϝϦοτ w݄1ʙ12 Date, Calendar0ʙ11 wtoStringͰISO 8601ܗࣜΛฦ͢ wͱ࣌ࠁΛ͢Δ͜ͱ͕Ͱ͖Δ wଟ༷ͳԋࢉػೳΛ࣋ͭ wεϨουηʔϑͰ͋Δ
σϝϦοτ w αΠζ͕େ͖͍ Ϋϥε͕ଟ͍ ˠJava SE APIͷதͰ࠷େڃ w طଘAPIͱͷޓੑʹ͍͠
w ࠓޙͷల։ JPAJAXBͷରԠ ͕ෆ໌ྎ w Early Draft Reviewޙසൟͳ༷มߋ
ࢀߟ: ISO 8601ܗࣜ w yyyy-MM-dd • 2012-11-10 w ࣌ࠁhh:mm:ss.SSSZ •
07:30:00.000Z w ࣌yyyy-MM-dd’T’HH:mm:ss.SSSZ • 2012-11-10T07:30:00.000Z
JSR 310 Essentials
Ϛγϯ͚ͷදݱ ෦දݱ • Instant • Duration • Clock ਓ͚ؒͷදݱ
֎෦දݱ • DateTime • Period • Chronology
Instant
Duration
Clock
DateTime : ɾ࣌ࠁΫϥε • DateTimeΠϯλϑΣʔεͷ࣮ • ݱࡏ࣌·ͨҙͷ͔࣌Βੜ • DateTimeͷԋࢉ(plus/minus)͓Αͼଞͷ DateTime͔ΒͷมΛαϙʔτ
• InstantʹରԠ͢Δਓ͚ؒදݱ
DateTimeΠϯλϑΣʔεͷ࣮ • LocalDate, LocalTime, LocalDateTime • OffsetDate, OffsetTime, OffsetDateTime •
ZonedDateTime • ෆใͷՃ·ͨ༨ใͷΓࣺ ͯʹΑΓ૬ޓʹมՄ
%BUF 5JNF %BUF5JNF -PDBM ݄ ! ࣌ࠩใͳ͠ ! ࣌ඵະຬ ࣌ࠩใͳ͠
݄ ࣌ඵະຬ ࣌ࠩใͳ͠ 0⒎TFU ݄ ! 65$͔Βͷ࣌ࠩ ! ࣌ඵະຬ 65$͔Βͷ࣌ࠩ ݄ ࣌ඵະຬ 65$͔Βͷ࣌ࠩ ;POFE /" /" ݄ ࣌ඵະຬ λΠϜκʔϯ
OffsetDateTime vs. ZonedDateTime wOffsetDateTimeUTC͔Βͷ࣌ࠩ wZonedDateTime࣮ࡍͷλΠϜκʔϯ ˞λΠϜκʔϯʹΑ͕ͬͯ࣌ࠩมಈ͢Δ wՆ࣌ؒಋೖࠃͰຖൃੜ wλΠϜκʔϯͷ࣌ࠩͷมߋ
Period : ظؒΫϥε wظؒΛද͢ ྫϲ݄ͱͱ࣌ؒ wϑΟʔϧυࢦఆɺͭͷDateTimeɺ Duration͔Βੜ wϑΟʔϧυऔಘɺPeriodͷplus/minusɺ DurationͷมͳͲΛαϙʔτ
wDurationʹରԠ͢Δਓ͚ؒදݱ
Chronology : ྐྵΫϥε • ChronologyΫϥε: ྐྵͷใΛอ࣋ɺChronoDateΛੜ • ChronoDateΫϥε: DateTimeΛϥοϓ֤ͯ͠ʑͷྐྵʹม ˞࠷৽ͷ࣮ͰෳΫϥεʹׂ
ChronoLocalDateɺChronoOffsetDateTime
ChronologyͷαϒΫϥε • ISOChronology - ISO 8601 ඪ४ • JapaneseChronologyྐྵ
ຊ • ThaiBuddhistChronologyྐྵ λΠ • MinguoChronologyຽࠃلݩ • HijrahChronologyΠεϥϜྐྵ
ɾ࣌ࠁԋࢉ wDateTimeɺPeriodɺInstantɺDuration ͦΕ͕ࣗՃࢉɾݮࢉػೳΛ࣋ͭ wϑΟʔϧυΛද͢Ϋϥεɾྻڍܕ͕֤छ ԋࢉΛఏڙ w Yearˠ͏Δ͏ఆ w Monthˠ݄ͷऔಘ w
DayOfWeekˠ༵औಘ
ϑΥʔϚοτػೳ wDateTimeFormatter: ग़ྗܗࣜΛΧελϚΠζ͢Δ wDateTimeFormatterBuilder͔Βൺֱత؆ ୯ʹDateTimeFormatterΛੜՄೳ wjava.text.DateFormatJSR 310Ͱ༻ Ͱ͖ͳ͍
Examples
ࠓͷΛऔಘˠEBUF LocalDate date = LocalDate.now();
݄ˠEBUF LocalDate date = LocalDate.of(2012, 11, 10);
ࠓͷˠE ޙͷˠE LocalDate d1 = LocalDate.now(); LocalDate d2 = d1.plusDays(3);
ࠓͷˠEBUF ࠓͷ࣌ˠEBUF5JNF LocalDate date = LocalDate.now(); LocalDateTime dateTime =
date.atTime(16, 30);
ࠓͷ࣌ˠEBUF5JNF ࠓͷˠEBUF LocalDateTime dateTime = LocalDateTime.now(); LocalDate date =
LocalDate.from(dateTime);
EBUF݄ ͏Δ͏ ˠMFBQUSVF LocalDate date = LocalDate.of(1992, 10, 8);
boolean leap = Year.from(date).isLeap();
ࠓͷˠEBUF EBUFΛඪ४ग़ྗ LocalDate date = LocalDate.now(); System.out.println(date.toString()); 2012-11-10 ग़ྗ݁Ռ
ࠓͷ ྐྵ ˠEBUF JapaneseChronology chrono = JapaneseChronology.INSTANCE; ChronoLocalDate<JapaneseChronology> jdate =
chrono.date(LocalDate.now()); System.out.println(jdate); H24-11-10 ग़ྗ݁Ռ
Compatibility
java.util.DateΛվम(OpenJDK8) wίϯετϥΫλDate(Instant instant) 㱻ίϯετϥΫλDate(long date) wInstant toInstant()ϝιου 㱻long getTime()ϝιου ˞java.util.Calendarʹಉ༷ͷվम
0⒎TFU%BUF5JNF͔Β*OTUBOUΛऔಘ औಘͨ͠*OTUBOU͔Β%BUFΛੜ OffsetDateTime dateTime = OffsetDateTime.now(); Instant instant =
dateTime.toInstant(); Date date = new Date(instant);
ଞͷAPIͱͷ࿈ܞ wকདྷతʹJPAɺJAXBల։༧ఆ wͨͩ͠ݱ࣌Ͱ۩ମతΞΫγϣϯͳ͠ wJPA࿈ܞૣظ࣮ݱΛࢦͯ͠ܭըதɺ ͱͷ͜ͱ ͍ͭʹͳΔ͜ͱΒʜ
Conclusion
JSR 310ͱԿͳͷ͔ wJSR 310java.util.DateͷܽΛͯ͢ ղܾ͠Α͏ͱ͢Δ৺తࢼΈ wաڈͱͷޓੑΛஅͪΔ͜ͱͰɺ ɾ࣌ࠁAPIͷཧΛٻͨ͠ wਖ਼Γա͗ײ൱Ίͳ͍͕ɺطଘAPI ͷͷଟ͘Λղܾͨ͠ධՁ
JSR 310ͷϦιʔε JSR 310 Specification http://jcp.org/en/jsr/detail?id=310 JSR 310 RI “ThreeTen”
http://threeten.sourceforge.net https://github.com/ThreeTen/threeten
Introduction to Date and Time API HASUNUMA Kenji
[email protected]
Twitter:
@btnrouge