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
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
130
Refactor your code - refactor yourself
xosofox
1
260
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
2
180
103 Early Hints
sugi_0000
1
230
Haze - Real time background blurring
chrisbanes
1
510
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
730
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
命名をリントする
chiroruxx
1
390
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Facilitating Awesome Meetings
lara
50
6.1k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
BBQ
matthewcrist
85
9.4k
Making the Leap to Tech Lead
cromwellryan
133
9k
Writing Fast Ruby
sferik
628
61k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Building Your Own Lightsaber
phodgson
103
6.1k
A better future with KSS
kneath
238
17k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
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