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
情報漏洩させないための設計
kubotak
4
1.1k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
280
Compose UIテストを使った統合テスト
hiroaki404
0
110
Spatial Rendering for Apple Vision Pro
warrenm
0
290
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
710
快速入門可觀測性
blueswen
0
460
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
2
520
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
770
return文におけるstd::moveについて
onihusube
1
1.3k
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
120
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
310
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.2k
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
9k
The Cult of Friendly URLs
andyhume
78
6.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Unsuck your backbone
ammeep
669
57k
Bash Introduction
62gerente
609
210k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Building an army of robots
kneath
302
44k
Building Adaptive Systems
keathley
38
2.3k
Producing Creativity
orderedlist
PRO
342
39k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
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