Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
18
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
35
Life of our small product
khasunuma
0
17
How to adapt MicroProfile API for generic Web applications
khasunuma
0
21
Overviewing Admin Console
khasunuma
0
19
Introduction to MicroProfile Metrics
khasunuma
0
42
Basic method for Java EE Web Profile
khasunuma
0
19
Introduction to JCA and MDB
khasunuma
0
53
Collections Framework Begineers Guide 2
khasunuma
0
55
JLS myths ~ if-then-else statement ~
khasunuma
0
19
Other Decks in Programming
See All in Programming
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
110
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Jakarta EE meets AI
ivargrimstad
0
620
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
910
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
イベント駆動で成長して委員会
happymana
1
320
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Jakarta EE meets AI
ivargrimstad
0
160
Featured
See All Featured
Faster Mobile Websites
deanohume
305
30k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
The Invisible Side of Design
smashingmag
298
50k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
A Tale of Four Properties
chriscoyier
156
23k
Docker and Python
trallard
40
3.1k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Documentation Writing (for coders)
carmenintech
65
4.4k
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