SlideShare a Scribd company logo
Introduction to
Date and Time API III
HASUNUMA Kenji
k.hasunuma@coppermine.jp

Twitter: @khasunuma
July 11, 2015
#kanjava
Time?
Definition of second (Traditional)
Definition of second (Modern)
time zones and offsets
+09:00
-08:00
PST (Pacific Standard Time)
Offset:-08:00 (Summer:-07:00)
JST (Japan Standard Time)
Offset:+09:00
ISO 8601
What's ISO 8601?
• International Standard

• Using Gregorian calendar

• Base of JIS X 0301, RFC 3339, etc.

• Incompatible with Unix time

(java.util.Date/Calendar are based on Unix time)
Time (w/o Time Zone)
• hh:mm:ss e.g. 14:30:15

• hh:mm e.g. 14:30

• hh e.g. 14

• hh:mm:ss.s e.g. 14:30:15.250
Time (w/Time Zone)
• Add suffix - offset from UTC (±hh:mm)

• hh:mm:ss±hh:mm

• e.g. 14:30:45+09:00 (Asia/Tokyo)

• e.g. 21:30:45-08:00 (America/Los_Angeles)

• e.g. 05:30:45Z (UTC)
Date
• calendar date: 

YYYY-MM-DD e.g. 2015-07-11

• ordinal date: 

YYYY-DDD e.g. 2015-192

• week date: 

YYYY-Www-D e.g. 2015-W29-6
Date (Short)
• year-month: 

YYYY-MM e.g. 2015-07

• year: 

YYYY e.g. 2015

• month-day: 

--MM-DD e.g. --07-11
Date and Time
• Concat date and time using 'T'

• If it needs, add offset (Time Zone)

• YYYY-MM-DDThh:mm:ss

e.g. 2015-07-11T14:45:30

• YYYY-MM-DDThh:mm:ss±hh:mm

e.g. 2015-07-10T21:45:30-08:00
Duration
• Time amount between time points

• date : nYnMnD e.g. 1Y3M22D

• time : nHnMnS e.g. 9H30M45S

• date and time : nYnMnDTnHnMnS

e.g. 1Y3M22DT9H30M45S
Period
• Range between dates/times

• YYYY-MM-DD/YYYY-MM-DD (start/end)

• YYYY-MM-DD/PnYnMnD (start/duration)

• PnYnMnD/YYYY-MM-DD (duration/end)

• PnYnMnD (duration)
Definition of week
• a week = 7 days

• 1st week contains 

the first Thursday of
the year.

• a year contents 52 or
53 weeks.
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
Date and Time API
Essentials
Packages
Package Description Use
java.time Basic classes usual
java.time.format Date and Time Formatter partial
java.time.chrono Chronology supports partial
java.time.temporal Low-level API rare
java.time.zone Low-level API rare
Date and Time classes
Class Field T/Z ISO 8601
LocalDate Date N/A YYYY-MM-DD
LocalDateTime Date/Time N/A YYYY-MM-DDThh:mm:ss
LocalTime Time N/A hh:mm:ss
OffsetDateTime Date/Time offset YYYY-MM-DDThh:mm:ss±hh:mm
OffsetTime Time offset hh:mm:ss±hh:mm
ZonedDateTime Date/Time zone id N/A
Factory methods
Oper. Description
of-
Create from fields.

e.g. LocalDate.of(2015, 7, 11)
now
Create from a clock.

e.g. LocalDate.now()
from
Create from other Temporal objects.

e.g. LocalDate.from(LocalDateTime.now())
parse
Create from String. (may use a formatter)

e.g. LocalDate.parse("2015-07-11")
"of" method (examples)
LocalDate.of(2015, 7, 11)

LocalDateTime.of(2015, 7, 11, 13, 30, 45, 250)

LocalTime.of(13, 30, 45, 250)

OffsetDateTime.of(2015, 7, 11, 13, 30, 45, 250, 

ZoneOffset.ofHours(9))

OffsetTime.of(13, 30, 45, 250, ZoneOffset.ofHours(9))

ZonedDateTime.of(2015, 7, 11, 13, 30, 45, 250, 

ZoneId.of("Asia/Tokyo"))
Conversion methods
Oper. Description
at-
Expand with fields.

e.g. LocalDate.of(2015, 7, 11).atTime(13, 30)

e.g. LocalDateTime.of(2015, 7, 11, 13, 30, 45)

.atOffset(ZoneOffset.ofHours(9))
to-
Truncate fields.

e.g. LocalDateTime.of(2015, 7, 11, 13, 30, 45)

.toLocalDate()
Date and Time conversions
Obtain/Modify methods
Oper. Description
get-
Obtain the value of a field.

e.g. LocalDate.now().getYear()

e.g. LocalDate.now().get(YEAR)
with-
Modify the value of a field (returns a copy).

e.g. LocalDate.now().withYear(2016)

e.g. LocalDate.now().with(2016, YEAR)
Obtain/Modify methods
ChronoField Obtain Modify
YEAR getYear withYear
MONTH_OF_YEAR getMonthValue withMonth
DAY_OF_MONTH getDayOfMonth withDayOfMonth
DAY_OF_WEEK getDayOfWeek N/A
HOUR_OF_DAY getHour withHour
MINUTE_OF_HOUR getMinute withMinute
SECOND_OD_MINUTE getSecond withSecond
NANO_OF_SECOND getNano withNano
Arithmetric methods
Oper. Description
plus-
Add a field value. (returns a copy)

e.g. LocalDate.now().plusDays(7)

e.g. LocalDate.now().plus(7, DAYS)
minus-
subtract a field value. (returns a copy)

e.g. LocalDate.now().minusDays(7)

e.g. LocalDate.now().minus(7, DAYS)
Arithmetric methods
ChronoUnit Add Subtract
YEARS plusYears minusYears
MONTHS plusMonths minusMonths
DAYS plusDays minusDays
WEEKS plusWeeks minusWeeks
HOURS plusHours minusHours
MINUTES plusMinutes minusMinutes
SECONDS plusSeconds minusSeconds
NANOS plusNanos minusNanos
Compare methods
Oper. Description
isBefore
e.g. today.isBefore(yesterday) ; false

e.g. today.isBefore(today) ; false

e.g. today.isBefore(tomorrow) ; true
isEqual
e.g. today.isEqual(yesterday) ; false

e.g. today.isEqual(today) ; true

e.g. today.isEqual(tomorrow) ; false
isAfter
e.g. today.isAfter(yesterday) ; true

e.g. today.isAfter(today) ; false

e.g. today.isAfter(tomorrow) ; false
Format/Parse methods
Method Description
toString()
Format using default formatter

(Instance method)
format(DateTimeFormatter f)
Format using custom formatter

(Instance method)
parse(String s)
Parse using default formatter

(Factory method)
parse(String s,
DateTimeFormatter f)
Parse using custom formatter

(Factory method)
DateTimeFormatter
1. Created by ofPattern factory (usually)

e.g. ofPettern("uuuu/MM/dd")

2. Select from pre-defined patterns:

• ISO_LOCAL_DATE

• ISO_OFFSET_TIME

• ISO_ZONED_DATE_TIME

3. Created by DateTimeFomatterBuilder
ResolverStyle
• One of formatter option (enum)

• LENIENT, SMART (default), STRICT

• Modifing method: withResolverStyle

• If it is STRICT mode, Pattern 'y' must
be used with 'G'

e.g. NO: yyyy/MM/dd OK: Gyyyy/MM/dd
Advanced
External representation
Representation for Human
Duration and Period
• Representation of temporal amount
correspond with "period" (ISO 8601).

• Period is the date part of "period", 

i.e. formatted as "P1Y2M3D"

• Duration is the time part of "period",
i.e. formatted as "PT15H30M45D"
Internal representation
Representation for Machine
Instant
• Representation of a time-point

• The precision is a nano second

• The epoch is 1970-01-01T00:00:00Z

• The only interface to java.util.Date
Clock
• Provider of the current instant.

• Zone relative, fixed and custom.

• By default, it uses the clock relative
current zone.

• now method (LocalDate, et al.) creates
a temporal instance from a clock.
Fixed Clock
• Clock always provides same instant.

• It's very useful for application testing.
Clock clock = Clock.fixed(instant, 

ZoneId.systemDefault);

!
// Using fixed clock

LocalDateTime.now(clock);
ZoneId and
ZoneOffset
ZoneId is ...
• ID/tag of a time zone.

• The representation of time zone is
ZoneRules.

• There is the system default value.

• Abstract class; derived to ZoneOffset
and ZoneRegion (implicit).
ZoneOffset is ...
• ID/tag of a time zone for fixed offsets.

• Contains the offset from UTC.

• Used for OffsetDateTime/OffsetTime.

• Used for ZonedDateTime (as ZoneId).

• Defines transitions of ZoneRules.
Class diagram
ZoneId.of(String zoneId)
1. Fixed offsets

e.g. "+09:00", "Z"

-> instance of ZoneOffset

2. Geographical regions

e.g. "Asia/Tokyo"

-> instance of ZoneRegion
Create ZoneId (examples)
• ZoneId.systemDefault()

• ZoneId.of("Asia/Tokyo")

• ZoneId.of("JST", ZoneId.SHORT_IDS)

• ZoneId.of("+09:00")

• ZoneId.from(ZonedDateTime.now())
OffsetDateTime vs.
ZonedDateTime
• OffsetDateTime is based on ISO 8601
but ZonedDateTime is not.

• ZonedDateTime is adapt to daylight
savings easily. OffsetDateTime is not.

• Zones may be changed because of
region or country convenience. But
offsets are never.
Conclusion
What's Date and Time API?
• Modeling of ISO 8601

• Many classes, but ease of use

• Powerful Date/Time calculations

(See also TemporalAdjuster)

• Many extention points

(See also java.time.chrono.*)
How to study?
• Learn ISO 8601 (JIS X 0301)

• Master to use LocalDate

• Know why exists Local/Offset/Zoned

• Set priority to the classes

• Trial and error!
Introduction to Date and Time API III
HASUNUMA Kenji
k.hasunuma@coppermine.jp

Twitter: @khasunuma

More Related Content

What's hot

A minimal introduction to Python non-uniform fast Fourier transform (pynufft)
A minimal introduction to Python non-uniform fast Fourier transform (pynufft)A minimal introduction to Python non-uniform fast Fourier transform (pynufft)
A minimal introduction to Python non-uniform fast Fourier transform (pynufft)
Jyh-Miin Lin
 
Gravitational Waves and Binary Systems (2) - Thibault Damour
Gravitational Waves and Binary Systems (2) - Thibault DamourGravitational Waves and Binary Systems (2) - Thibault Damour
Gravitational Waves and Binary Systems (2) - Thibault Damour
Lake Como School of Advanced Studies
 
Colored inversion
Colored inversionColored inversion
Colored inversion
Jalal Neshat
 
Public Wi-Fi
Public Wi-FiPublic Wi-Fi
Public Wi-Fi
Hiroshi Mano
 
Ee2365 nol part 2
Ee2365 nol part 2Ee2365 nol part 2
Ee2365 nol part 2
Arun Kumaar
 
Blaha krakow 2004
Blaha krakow 2004Blaha krakow 2004
Blaha krakow 2004
ABDERRAHMANE REGGAD
 
4.3 real time game physics
4.3 real time game physics4.3 real time game physics
4.3 real time game physics
Sayed Ahmed
 
Deep parking
Deep parkingDeep parking
Deep parking
Shintaro Shiba
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorch
Jun Young Park
 
Wien2k getting started
Wien2k getting startedWien2k getting started
Wien2k getting started
ABDERRAHMANE REGGAD
 
First and second order semi-Markov chains for wind speed modeling
First and second order semi-Markov chains for wind speed modelingFirst and second order semi-Markov chains for wind speed modeling
First and second order semi-Markov chains for wind speed modeling
Nozir Shokirov
 
time response
time responsetime response
time response
University Malaya
 
Localized Electrons with Wien2k
Localized Electrons with Wien2kLocalized Electrons with Wien2k
Localized Electrons with Wien2k
ABDERRAHMANE REGGAD
 
Mathematics Colloquium, UCSC
Mathematics Colloquium, UCSCMathematics Colloquium, UCSC
Mathematics Colloquium, UCSC
dongwook159
 
Blind separation of complex-valued satellite-AIS data for marine surveillance...
Blind separation of complex-valued satellite-AIS data for marine surveillance...Blind separation of complex-valued satellite-AIS data for marine surveillance...
Blind separation of complex-valued satellite-AIS data for marine surveillance...
IJECEIAES
 
high_energy_physics_UCARE_poster
high_energy_physics_UCARE_posterhigh_energy_physics_UCARE_poster
high_energy_physics_UCARE_posterMaxwell Gregoire
 
A parallel gpu version of the traveling salesman problem slides
A parallel gpu version of the traveling salesman problem slidesA parallel gpu version of the traveling salesman problem slides
A parallel gpu version of the traveling salesman problem slides
Vimukthi Wickramasinghe
 

What's hot (18)

A minimal introduction to Python non-uniform fast Fourier transform (pynufft)
A minimal introduction to Python non-uniform fast Fourier transform (pynufft)A minimal introduction to Python non-uniform fast Fourier transform (pynufft)
A minimal introduction to Python non-uniform fast Fourier transform (pynufft)
 
Bode lect
Bode lectBode lect
Bode lect
 
Gravitational Waves and Binary Systems (2) - Thibault Damour
Gravitational Waves and Binary Systems (2) - Thibault DamourGravitational Waves and Binary Systems (2) - Thibault Damour
Gravitational Waves and Binary Systems (2) - Thibault Damour
 
Colored inversion
Colored inversionColored inversion
Colored inversion
 
Public Wi-Fi
Public Wi-FiPublic Wi-Fi
Public Wi-Fi
 
Ee2365 nol part 2
Ee2365 nol part 2Ee2365 nol part 2
Ee2365 nol part 2
 
Blaha krakow 2004
Blaha krakow 2004Blaha krakow 2004
Blaha krakow 2004
 
4.3 real time game physics
4.3 real time game physics4.3 real time game physics
4.3 real time game physics
 
Deep parking
Deep parkingDeep parking
Deep parking
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorch
 
Wien2k getting started
Wien2k getting startedWien2k getting started
Wien2k getting started
 
First and second order semi-Markov chains for wind speed modeling
First and second order semi-Markov chains for wind speed modelingFirst and second order semi-Markov chains for wind speed modeling
First and second order semi-Markov chains for wind speed modeling
 
time response
time responsetime response
time response
 
Localized Electrons with Wien2k
Localized Electrons with Wien2kLocalized Electrons with Wien2k
Localized Electrons with Wien2k
 
Mathematics Colloquium, UCSC
Mathematics Colloquium, UCSCMathematics Colloquium, UCSC
Mathematics Colloquium, UCSC
 
Blind separation of complex-valued satellite-AIS data for marine surveillance...
Blind separation of complex-valued satellite-AIS data for marine surveillance...Blind separation of complex-valued satellite-AIS data for marine surveillance...
Blind separation of complex-valued satellite-AIS data for marine surveillance...
 
high_energy_physics_UCARE_poster
high_energy_physics_UCARE_posterhigh_energy_physics_UCARE_poster
high_energy_physics_UCARE_poster
 
A parallel gpu version of the traveling salesman problem slides
A parallel gpu version of the traveling salesman problem slidesA parallel gpu version of the traveling salesman problem slides
A parallel gpu version of the traveling salesman problem slides
 

Similar to Introduction to Date and Time API 3

Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3
Kenji HASUNUMA
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
Kenji HASUNUMA
 
Brand New Date and Time API
Brand New Date and Time APIBrand New Date and Time API
Brand New Date and Time API
Kenji HASUNUMA
 
Java 8 Date and Time API
Java 8 Date and Time APIJava 8 Date and Time API
Java 8 Date and Time API
Ganesh Samarthyam
 
jkfdlsajfklafj
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
PlanetExpressATX
 
Lesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptxLesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptx
LagamaPasala
 
Fun times with ruby
Fun times with rubyFun times with ruby
Fun times with ruby
Geoff Harcourt
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
Serhii Kartashov
 
15. DateTime API.ppt
15. DateTime API.ppt15. DateTime API.ppt
15. DateTime API.ppt
VISHNUSHANKARSINGH3
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
Amrit Kaur
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
allanh0526
 
Php date & time functions
Php date & time functionsPhp date & time functions
Php date & time functions
Programmer Blog
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulationShahjahan Samoon
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
ssuser034ce1
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
Michael Arenzon
 
Date object.pptx date and object v
Date object.pptx date and object        vDate object.pptx date and object        v
Date object.pptx date and object v
22x026
 
Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8
Fulvio Corno
 
Zombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the UndeadZombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the Undead
Stephen Chin
 
Motion and time study english
Motion and time study englishMotion and time study english
Motion and time study english
Norhan Abd Elaziz
 

Similar to Introduction to Date and Time API 3 (20)

Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
Brand New Date and Time API
Brand New Date and Time APIBrand New Date and Time API
Brand New Date and Time API
 
Java 8 Date and Time API
Java 8 Date and Time APIJava 8 Date and Time API
Java 8 Date and Time API
 
doc
docdoc
doc
 
jkfdlsajfklafj
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
 
Lesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptxLesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptx
 
Fun times with ruby
Fun times with rubyFun times with ruby
Fun times with ruby
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
 
15. DateTime API.ppt
15. DateTime API.ppt15. DateTime API.ppt
15. DateTime API.ppt
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
 
Php date & time functions
Php date & time functionsPhp date & time functions
Php date & time functions
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulation
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
 
Date object.pptx date and object v
Date object.pptx date and object        vDate object.pptx date and object        v
Date object.pptx date and object v
 
Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8
 
Zombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the UndeadZombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the Undead
 
Motion and time study english
Motion and time study englishMotion and time study english
Motion and time study english
 

More from Kenji HASUNUMA

oop-in-javaee
oop-in-javaeeoop-in-javaee
oop-in-javaee
Kenji HASUNUMA
 
Jakarta REST in depth
Jakarta REST in depthJakarta REST in depth
Jakarta REST in depth
Kenji HASUNUMA
 
Life of our small product
Life of our small productLife of our small product
Life of our small product
Kenji HASUNUMA
 
Jakarta EE : The First Parts
Jakarta EE : The First PartsJakarta EE : The First Parts
Jakarta EE : The First Parts
Kenji HASUNUMA
 
Overviewing Admin Console
Overviewing Admin ConsoleOverviewing Admin Console
Overviewing Admin Console
Kenji HASUNUMA
 
How to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web ApplicationsHow to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web Applications
Kenji HASUNUMA
 
Introduction to MicroProfile Metrics
Introduction to MicroProfile MetricsIntroduction to MicroProfile Metrics
Introduction to MicroProfile Metrics
Kenji HASUNUMA
 
Introduction to JCA and MDB
Introduction to JCA and MDBIntroduction to JCA and MDB
Introduction to JCA and MDB
Kenji HASUNUMA
 
Virtualization Fundamental
Virtualization FundamentalVirtualization Fundamental
Virtualization Fundamental
Kenji HASUNUMA
 
JLS myths
JLS mythsJLS myths
JLS myths
Kenji HASUNUMA
 
Fundamental Java
Fundamental JavaFundamental Java
Fundamental Java
Kenji HASUNUMA
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
Kenji HASUNUMA
 
Introduction to JavaFX Dialogs
Introduction to JavaFX DialogsIntroduction to JavaFX Dialogs
Introduction to JavaFX Dialogs
Kenji HASUNUMA
 
Introduction to Date and Time API 2
Introduction to Date and Time API 2Introduction to Date and Time API 2
Introduction to Date and Time API 2
Kenji HASUNUMA
 
Introduction to Data and Time API
Introduction to Data and Time APIIntroduction to Data and Time API
Introduction to Data and Time API
Kenji HASUNUMA
 

More from Kenji HASUNUMA (15)

oop-in-javaee
oop-in-javaeeoop-in-javaee
oop-in-javaee
 
Jakarta REST in depth
Jakarta REST in depthJakarta REST in depth
Jakarta REST in depth
 
Life of our small product
Life of our small productLife of our small product
Life of our small product
 
Jakarta EE : The First Parts
Jakarta EE : The First PartsJakarta EE : The First Parts
Jakarta EE : The First Parts
 
Overviewing Admin Console
Overviewing Admin ConsoleOverviewing Admin Console
Overviewing Admin Console
 
How to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web ApplicationsHow to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web Applications
 
Introduction to MicroProfile Metrics
Introduction to MicroProfile MetricsIntroduction to MicroProfile Metrics
Introduction to MicroProfile Metrics
 
Introduction to JCA and MDB
Introduction to JCA and MDBIntroduction to JCA and MDB
Introduction to JCA and MDB
 
Virtualization Fundamental
Virtualization FundamentalVirtualization Fundamental
Virtualization Fundamental
 
JLS myths
JLS mythsJLS myths
JLS myths
 
Fundamental Java
Fundamental JavaFundamental Java
Fundamental Java
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
 
Introduction to JavaFX Dialogs
Introduction to JavaFX DialogsIntroduction to JavaFX Dialogs
Introduction to JavaFX Dialogs
 
Introduction to Date and Time API 2
Introduction to Date and Time API 2Introduction to Date and Time API 2
Introduction to Date and Time API 2
 
Introduction to Data and Time API
Introduction to Data and Time APIIntroduction to Data and Time API
Introduction to Data and Time API
 

Recently uploaded

Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 

Recently uploaded (20)

Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 

Introduction to Date and Time API 3

  • 1. Introduction to Date and Time API III HASUNUMA Kenji k.hasunuma@coppermine.jp Twitter: @khasunuma July 11, 2015 #kanjava
  • 3.
  • 4. Definition of second (Traditional)
  • 6. time zones and offsets +09:00 -08:00
  • 7. PST (Pacific Standard Time) Offset:-08:00 (Summer:-07:00) JST (Japan Standard Time) Offset:+09:00
  • 9. What's ISO 8601? • International Standard • Using Gregorian calendar • Base of JIS X 0301, RFC 3339, etc. • Incompatible with Unix time
 (java.util.Date/Calendar are based on Unix time)
  • 10. Time (w/o Time Zone) • hh:mm:ss e.g. 14:30:15 • hh:mm e.g. 14:30 • hh e.g. 14 • hh:mm:ss.s e.g. 14:30:15.250
  • 11. Time (w/Time Zone) • Add suffix - offset from UTC (±hh:mm) • hh:mm:ss±hh:mm • e.g. 14:30:45+09:00 (Asia/Tokyo) • e.g. 21:30:45-08:00 (America/Los_Angeles) • e.g. 05:30:45Z (UTC)
  • 12. Date • calendar date: 
 YYYY-MM-DD e.g. 2015-07-11 • ordinal date: 
 YYYY-DDD e.g. 2015-192 • week date: 
 YYYY-Www-D e.g. 2015-W29-6
  • 13. Date (Short) • year-month: 
 YYYY-MM e.g. 2015-07 • year: 
 YYYY e.g. 2015 • month-day: 
 --MM-DD e.g. --07-11
  • 14. Date and Time • Concat date and time using 'T' • If it needs, add offset (Time Zone) • YYYY-MM-DDThh:mm:ss
 e.g. 2015-07-11T14:45:30 • YYYY-MM-DDThh:mm:ss±hh:mm
 e.g. 2015-07-10T21:45:30-08:00
  • 15. Duration • Time amount between time points • date : nYnMnD e.g. 1Y3M22D • time : nHnMnS e.g. 9H30M45S • date and time : nYnMnDTnHnMnS
 e.g. 1Y3M22DT9H30M45S
  • 16. Period • Range between dates/times • YYYY-MM-DD/YYYY-MM-DD (start/end) • YYYY-MM-DD/PnYnMnD (start/duration) • PnYnMnD/YYYY-MM-DD (duration/end) • PnYnMnD (duration)
  • 17. Definition of week • a week = 7 days • 1st week contains 
 the first Thursday of the year. • a year contents 52 or 53 weeks. 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 Sunday
  • 18. Date and Time API Essentials
  • 19. Packages Package Description Use java.time Basic classes usual java.time.format Date and Time Formatter partial java.time.chrono Chronology supports partial java.time.temporal Low-level API rare java.time.zone Low-level API rare
  • 20. Date and Time classes Class Field T/Z ISO 8601 LocalDate Date N/A YYYY-MM-DD LocalDateTime Date/Time N/A YYYY-MM-DDThh:mm:ss LocalTime Time N/A hh:mm:ss OffsetDateTime Date/Time offset YYYY-MM-DDThh:mm:ss±hh:mm OffsetTime Time offset hh:mm:ss±hh:mm ZonedDateTime Date/Time zone id N/A
  • 21. Factory methods Oper. Description of- Create from fields. e.g. LocalDate.of(2015, 7, 11) now Create from a clock. e.g. LocalDate.now() from Create from other Temporal objects. e.g. LocalDate.from(LocalDateTime.now()) parse Create from String. (may use a formatter) e.g. LocalDate.parse("2015-07-11")
  • 22. "of" method (examples) LocalDate.of(2015, 7, 11) LocalDateTime.of(2015, 7, 11, 13, 30, 45, 250) LocalTime.of(13, 30, 45, 250) OffsetDateTime.of(2015, 7, 11, 13, 30, 45, 250, 
 ZoneOffset.ofHours(9)) OffsetTime.of(13, 30, 45, 250, ZoneOffset.ofHours(9)) ZonedDateTime.of(2015, 7, 11, 13, 30, 45, 250, 
 ZoneId.of("Asia/Tokyo"))
  • 23. Conversion methods Oper. Description at- Expand with fields. e.g. LocalDate.of(2015, 7, 11).atTime(13, 30) e.g. LocalDateTime.of(2015, 7, 11, 13, 30, 45)
 .atOffset(ZoneOffset.ofHours(9)) to- Truncate fields. e.g. LocalDateTime.of(2015, 7, 11, 13, 30, 45)
 .toLocalDate()
  • 24. Date and Time conversions
  • 25. Obtain/Modify methods Oper. Description get- Obtain the value of a field. e.g. LocalDate.now().getYear() e.g. LocalDate.now().get(YEAR) with- Modify the value of a field (returns a copy). e.g. LocalDate.now().withYear(2016) e.g. LocalDate.now().with(2016, YEAR)
  • 26. Obtain/Modify methods ChronoField Obtain Modify YEAR getYear withYear MONTH_OF_YEAR getMonthValue withMonth DAY_OF_MONTH getDayOfMonth withDayOfMonth DAY_OF_WEEK getDayOfWeek N/A HOUR_OF_DAY getHour withHour MINUTE_OF_HOUR getMinute withMinute SECOND_OD_MINUTE getSecond withSecond NANO_OF_SECOND getNano withNano
  • 27. Arithmetric methods Oper. Description plus- Add a field value. (returns a copy) e.g. LocalDate.now().plusDays(7) e.g. LocalDate.now().plus(7, DAYS) minus- subtract a field value. (returns a copy) e.g. LocalDate.now().minusDays(7) e.g. LocalDate.now().minus(7, DAYS)
  • 28. Arithmetric methods ChronoUnit Add Subtract YEARS plusYears minusYears MONTHS plusMonths minusMonths DAYS plusDays minusDays WEEKS plusWeeks minusWeeks HOURS plusHours minusHours MINUTES plusMinutes minusMinutes SECONDS plusSeconds minusSeconds NANOS plusNanos minusNanos
  • 29. Compare methods Oper. Description isBefore e.g. today.isBefore(yesterday) ; false e.g. today.isBefore(today) ; false e.g. today.isBefore(tomorrow) ; true isEqual e.g. today.isEqual(yesterday) ; false e.g. today.isEqual(today) ; true e.g. today.isEqual(tomorrow) ; false isAfter e.g. today.isAfter(yesterday) ; true e.g. today.isAfter(today) ; false e.g. today.isAfter(tomorrow) ; false
  • 30. Format/Parse methods Method Description toString() Format using default formatter (Instance method) format(DateTimeFormatter f) Format using custom formatter (Instance method) parse(String s) Parse using default formatter (Factory method) parse(String s, DateTimeFormatter f) Parse using custom formatter (Factory method)
  • 31. DateTimeFormatter 1. Created by ofPattern factory (usually)
 e.g. ofPettern("uuuu/MM/dd") 2. Select from pre-defined patterns: • ISO_LOCAL_DATE • ISO_OFFSET_TIME • ISO_ZONED_DATE_TIME 3. Created by DateTimeFomatterBuilder
  • 32. ResolverStyle • One of formatter option (enum) • LENIENT, SMART (default), STRICT • Modifing method: withResolverStyle • If it is STRICT mode, Pattern 'y' must be used with 'G'
 e.g. NO: yyyy/MM/dd OK: Gyyyy/MM/dd
  • 35. Duration and Period • Representation of temporal amount correspond with "period" (ISO 8601). • Period is the date part of "period", 
 i.e. formatted as "P1Y2M3D" • Duration is the time part of "period", i.e. formatted as "PT15H30M45D"
  • 37. Instant • Representation of a time-point • The precision is a nano second • The epoch is 1970-01-01T00:00:00Z • The only interface to java.util.Date
  • 38. Clock • Provider of the current instant. • Zone relative, fixed and custom. • By default, it uses the clock relative current zone. • now method (LocalDate, et al.) creates a temporal instance from a clock.
  • 39. Fixed Clock • Clock always provides same instant. • It's very useful for application testing. Clock clock = Clock.fixed(instant, 
 ZoneId.systemDefault); ! // Using fixed clock LocalDateTime.now(clock);
  • 41. ZoneId is ... • ID/tag of a time zone. • The representation of time zone is ZoneRules. • There is the system default value. • Abstract class; derived to ZoneOffset and ZoneRegion (implicit).
  • 42. ZoneOffset is ... • ID/tag of a time zone for fixed offsets. • Contains the offset from UTC. • Used for OffsetDateTime/OffsetTime. • Used for ZonedDateTime (as ZoneId). • Defines transitions of ZoneRules.
  • 44. ZoneId.of(String zoneId) 1. Fixed offsets
 e.g. "+09:00", "Z"
 -> instance of ZoneOffset 2. Geographical regions
 e.g. "Asia/Tokyo"
 -> instance of ZoneRegion
  • 45. Create ZoneId (examples) • ZoneId.systemDefault() • ZoneId.of("Asia/Tokyo") • ZoneId.of("JST", ZoneId.SHORT_IDS) • ZoneId.of("+09:00") • ZoneId.from(ZonedDateTime.now())
  • 46. OffsetDateTime vs. ZonedDateTime • OffsetDateTime is based on ISO 8601 but ZonedDateTime is not. • ZonedDateTime is adapt to daylight savings easily. OffsetDateTime is not. • Zones may be changed because of region or country convenience. But offsets are never.
  • 48. What's Date and Time API? • Modeling of ISO 8601 • Many classes, but ease of use • Powerful Date/Time calculations
 (See also TemporalAdjuster) • Many extention points
 (See also java.time.chrono.*)
  • 49. How to study? • Learn ISO 8601 (JIS X 0301) • Master to use LocalDate • Know why exists Local/Offset/Zoned • Set priority to the classes • Trial and error!
  • 50. Introduction to Date and Time API III HASUNUMA Kenji k.hasunuma@coppermine.jp Twitter: @khasunuma