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
初めてDefinitelyTypedにPRを出した話
syumai
0
420
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
subpath importsで始めるモック生活
10tera
0
310
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
940
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
Click-free releases & the making of a CLI app
oheyadam
2
120
CSC509 Lecture 12
javiergs
PRO
0
160
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Thoughts on Productivity
jonyablonski
67
4.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Scaling GitHub
holman
458
140k
A Philosophy of Restraint
colly
203
16k
Designing the Hi-DPI Web
ddemaree
280
34k
What's new in Ruby 2.0
geeforr
343
31k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
A Tale of Four Properties
chriscoyier
156
23k
The Art of Programming - Codeland 2020
erikaheidi
52
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