SlideShare a Scribd company logo
Brand new
Date and Time API
March 21, 2014
HASUNUMA Kenji

khasunuma@java.net
http://www.coppermine.jp/
Twitter:
Preface
• JSR 310 is a new and improved date and time API
for Java.
• It is based on ISO 8601 standard.
• The reference implementation is called "ThreeTen".
• Specification Leads: Stephen Colebourne, Michael
Nascimento Santos and Roger Riggs.
What is time?

“Time” is described by science approaches and defined by
International Standard (SI) exactly.
Skip
Definition of second (Traditional)
Definition of second (Modern)
PST (Pacific Standard Time)
Offset:-08:00 (Summer:-07:00)
JST (Japan Standard Time)
Offset:+09:00
Skip
Time zones (East 1/2)
Code Name Offset Summer
ECT Europe/Paris +01:00 +02:00
CAT Africa/Harare +02:00 (N/A)
ART Africa/Cairo +02:00 (N/A)
EAT Africa/Addis_Abeba +03:00 (N/A)
NET Asia/Yerevan +04:00 (N/A)
PLT Asia/Karachi +05:00 (N/A)
IST Asia/Koltaka +05:30 (N/A)
BST Asia/Dacca +06:00 (N/A)
Time zones (East 2/2)
Code Name Offset Summer
VST Asia/Ho_Chi_Minh +07:00 (N/A)
CTT Asia/Shanghai +08:00 (N/A)
JST Asia/Tokyo +09:00 (N/A)
ACT Australia/Darwin +09:30 (N/A)
AET Australia/Sydney +11:00 +10:00
SST Pacific/Guadalcanal +11:00 (N/A)
NST Pacific/Auckland +13:00 +12:00
MIT Pacific/Apia +14:00 +13:00
Time zones (West 1/2)
Code Name Offset Summer
BET America/Sao_Paulo -02:00 -03:00
AGT America/Argentina/Buenos_Aires -03:00 (N/A)
CNT America/St_Johns -03:30 -02:30
PRT America/Puerto_Rico -04:00 (N/A)
EST (N/A) -05:00 (N/A)
IET America/Indiana/Indianapolis -05:00 -04:00
Time zones (West 2/2)
Code Name Offset Summer
CST America/Chicago -06:00 -05:00
MST (N/A) -07:00 (N/A)
PNT America/Phoenix -07:00 (N/A)
PST America/Los_Angeles -08:00 -07:00
AST America/Anchorage -09:00 -08:00
HST (N/A) -10:00 (N/A)
ISO 8601

Date elements and interchange formats - Information
interchange - Representation of dates and times
Skip
What’s ISO 8601?
• International standard for representation of dates
and times, using information interchange.
• Based on Gregorian calendar system (1582).
• JIS X 0301 is Japanese translations with remarks
about Japanese chronology.
• ISO 8601 and Unix time are incompatible.

Unix time is basis of java.util.Date, et al.
Representation of time

(w/o Time zone)
• hh:mm:ss e.g. 15:30:45
• hh:mm e.g. 15:30
• hh e.g. 15
• hh:mm:ss.s e.g. 15:30:45.250
Representation of time

(w/Time zone)
• Add suffix based on the offset from UTC
• UTC: ‘Z’ e.g. 06:30:45Z
• Not UTC: +hh:mm or -hh:mm

e.g. 15:30:45+09:00 (JST, Asia/Tokyo)

02:30:45-08:00 (PST, America/Los_Angeles)
Representation of date
• date - it consists with year, month, day and week.
• calendar date YYYY-MM-DD e.g. 2014-03-21
• ordinal date YYYY-DDD e.g. 2014-080
• week date YYYY-Www-D e.g. 2014-W12-5
• Short representations:
• year-month YYYY-MM e.g. 2014-03
• year YYYY e.g. 2014
• century YY e.g. 20
• month-day --MM-DD e.g. --03-21
Definition of day
• a day = 24 hours = 86,400 seconds
• 00:00~24:00 (24:00 is same as next 00:00)
• Exceptions:
• Leap seconds
• Begin and end of summer time
• When the time zone is changed
Definition of month
• a month = 28, 29, 30 or 31 days
1 January 01~31 7 July 01~31
2 February 01~28/29 8 August 01~31
3 March 01~31 9 September 01~30
4 April 01~30 10 October 01~31
5 May 01~31 11 November 01~30
6 June 01~30 12 December 01~31
Definition of year
• a year = approximation days while the orbital period
of Earth (365.242 19 days), 12 months.
• common year = 365 days
• leap year = 366 days
• 0000~9999 (0000~1582 is reserved)
mod 4 mod 100 mod 400 year
Not Zero - - common year
Zero Not Zero - leap year
Zero Zero Not Zero common year
Zero Zero Zero leap year
Definition of week
• a week = 7 days
• 1st week of year = a week
contents 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
Origin of week
Representation of date and time
• concat date and time by 'T'.
• YYYY-MM-DDThh:mm:ss[Time zone]
• Alt. YYYY-DDDThh:mm:ss[Time Zone]
• e.g.
2014-03-21T06:30
2014-03-21T15:30:45+09:00
2014-079T02:30:45-08:00
Definition and representation
of duration
• time amount between two time points
• date : nYnMnD e.g. 1Y3M22D
• time : nHnMnS e.g. 9H30M45S
• date and time : nYnMnDTnHnMnS

e.g. 1Y3M22DT9H30M45S
Definition and representation
of period
• range between two dates (adopt also two times)
• two dates : YYYY-MM-DD/YYYY-MM-DD

e.g. 2013-12-31/2014-03-21
• start date and duration : YYYY-MM-DD/PnYnMnD

e.g. 2013-12-31/P1Y3M22D
• end date and duration : PnYnMnD/YYYY-MM-DD

e.g. P1Y3M22D/2013-03-21
• duration : PnYnMnD

e.g. P1Y3M22D
JSR 310

Date and Time API
Skip
JSR 310 : Date and Time API
• Since 2007, Reference Implementation: ThreeTen
• Relational projects:
• ThreeTen-Extra: Features dropped from JDK8, 

i.e. UTC/TAI full support, Coptic chronology, et al.

https://github.com/ThreeTen/threeten-extra
• ThreeTen-Backport: JSR 310 like API for JDK7.

https://github.com/ThreeTen/threetenbp
• Joda-Time: Provides many hints to JSR 310.

http://www.joda.org/joda-time/
Package java.time.*
Package Description Use
java.time Basic classes usually
java.time.format Date and Time Formatter often
java.time.chrono Chronology supports partially
java.time.temporal Low-level API rarely
java.time.zone Low-level API rarely
Date and Time classes
Class Field Time zone ISO 8601 Chrono.
LocalDate Date N/A compatible enable
LocalDateTime Date + Time N/A compatible enable
LocalTime Time N/A compatible N/A
OffsetDateTime Date + Time offset compatible disable
OffsetTime Time offset compatible N/A
ZonedDateTime Date + Time zone id extended enable
Date and Time conversions
Date and Time conversions
Oper. Description
of-
Create from fields.
e.g. LocalDate today = LocalDate.of(2014, 3, 21);
// today.toString() : “2014-03-21”
at-
Expand with fields.
e.g. LocalDateTime current = today.atTime(15:30);
// current.toString() : “2014-03-21T15:30:00”
to-
Truncate fields.
e.g. LocalDate someday = current.toLocalDate();
// someday.toString() : “2014-03-21”
Date and Time operators
Oper. Description
isBefore
isEqual
isAfter
Compare with other date/time.
e.g. LocalDate today = LocalDate.of(2014, 3, 21);
boolean b = today.isAfter(LocalDate.of(2014, 3, 19));
// b == true
plus-
minus-
Add/subtract field value.
e.g. LocalDate tomorrow = today.plusDays(2);
// tomorrow.toString() : “2014-03-23”
isLeapYear
Verify a date is leap year.
e.g. boolean leap = today.isLeapYear();
// leap == false
Date and Time formatting
Method Description
toString()
Format using default formatter.
e.g. String s = today.toString();
// s : “2014-03-21”
format(DateTimeFormatter f)
Format using custom formatter.
e.g. String s = today.format(formatter);
parse(String s)
Parse using default formatter.
e.g. LocalDate d = LocalDate

.parse(“2014-03-21”);
parse(String s,
DateTimeFormatter f)
Parse using custom formatter.
e.g. LocalDate d = LocalDate

.parse(“2014-03-21”, formatter);
Chronology
• Date and Time API supports ISO 8601 and some local
chronology (e.g., Japanese Era, Minguo Era, Thai
Buddhist Era and Hijrah Era).
• ChronoLocalDate and its sub-classes (incl. LocalDate)
support chronology.
• ChronoLocalDateTime<D>/ChronoZonedDateTime<D>
instead of LocalDateTime/ZonedDateTime
• dates/times that have different chronology can convert
by from method each other.
Chronology supports
Chronology Era ChronoLocalDate
IsoChronology IsoEra LocalDate
JapaneseChronology JapaneseEra JapaneseDate
MinguoChronology MinguoEra MinguoDate
ThaiBuddhistChronology ThaiBuddhistEra ThaiBuddhistDate
HijrahChronology HijrahEra HijrahDate
Chronology examples
LocalDate today = LocalDate.of(2014, 3, 21);
System.out.println(today);
System.out.println(JapaneseDate.from(today));
System.out.println(MinguoDate.from(today));
System.out.println(ThaiBuddhistDate.from(today));
System.out.println(HijrahDate.from(today));
2014-03-21
Japanese Heisei 26-03-21
Minguo ROC 103-03-21
ThaiBuddhist BE 2557-03-21
Hijrah-umalqura AH 1435-05-20
DateTimeFormatter
• Provides formats date/time.
• Created by ofPattern method (basic way)

e.g. DateTimeFormatter.ofPattern(“yyyy/MM/dd”)
• Created by DateTimeFormatterBuilder (full control)
• Pre-defined patterns (static fields)
• ISO_LOCAL_DATE // ofPattern(“yyyy-MM-dd”)
• ISO_ORDINAL_DATE // ofPattern(“yyyy-ddd”)
• ISO_WEEK_DATE // ofPattern(“yyyy-Www”)
Chronology and Formatter

e.g. JapaneseDate
pattern display example
"YYYY/MM/dd" 2014/03/21
"Y/M/d" 2014/3/21
"GGGGGyy.MM.dd" H26.03.21
"Gy年M月d日(E)" 平成26年3月21日(金)
External representation

(for Human)
Internal representation

(for Machine)
Instant
• Representation of a time-point.
• The precision is a nano second.
• The epoch is 1970-01-01T00:00:00Z
• Convert from/to LocalDateTime, OffsetDateTime or
ZonedDateTime.
• Convert from/to java.util.Date, which means that
Instant is the only interface to java.util.Date.
Duration and Period
• Representations of period (ISO 8601).
• Period (JSR 310) is a period (ISO 8601) between two
dates and represents as a date-scale.

i.e. format as "P1Y2M3D"
• Duration (JSR 310) is a period (ISO 8601) between
any two temporals and represents as a time-scale.

i.e. format as "PT15H30M45D"
• Attention: those definitions are different from them of
ISO 8601.
Clock and now method
• Provider of the current instant.
• There are some clocks, having relation of zones (incl.
current zone), fixed clock (for testing) and custom.
• By default, it uses the clock of current zone.
• now method (LocalDate, et al.) creates a temporal
instance from a clock. 

e.g. LocalDate.now(); 

LocalDate.now(zoneId); 

LocalDate.now(Clock.fixed(instant, ZoneId.systemDefault);
Examples

More examples:
https://github.com/btnrouge/threetensamples
Skip
// It is assumed to run at March 21, 2014
LocalDate today = LocalDate.new();
!
System.out.println(today);
2014-03-21
LocalDate calendarDate
= LocalDate.of(2014, 3, 21);
LocalDate ordinalDate
= LocalDate.ofYearDays(2014, 80);
LocalDateTime localDateTime
= LocalDateTime.of(2014, 3, 21, 15, 30);
!
System.out.println(calendarDate);
System.out.println(ordinalDate);
System.out.println(localDateTime);
2014-03-21
2014-03-21
2014-03-21T15:30
OffsetDateTime offsetDateTime
= OffsetDateTime.of(2014, 3, 21,
15, 30, 45, 0,
ZoneOffset.ofHours(9));
ZonedDateTime zonedDateTime
= ZonedDateTime.of(2014, 3, 21,
15, 30, 45, 0,
ZoneId.of("Asia/Tokyo"));
!
System.out.println(offsetDateTime);
System.out.println(zonedDateTime);
2014-03-21T15:30:45+09:00
2014-03-21T15:30:45+09:00[Asia/Tokyo]
LocalDate localDate
= LocalDate.of(2014, 3, 21);
LocalDateTime localDateTime0
= localDate.atStartOfDay();
LocalDateTime localDateTime1
= localDate.atTime(15, 30, 45);
OffsetDateTime offsetDateTime
= localDateTime.atOffset(
ZoneOffset.ofHours(9));
!
System.out.println(localDateTime0);
System.out.println(localDateTime1);
System.out.println(offsetDateTime);
2014-03-21T00:00
2014-03-21T15:30:45
2014-03-21T15:30:45+09:00
OffsetDateTime offsetDateTime
= OffsetDateTime.of(2014, 3, 21,
15, 30, 45, 0,
ZoneOffset.ofHours(9));
LocalDate localDate =
offsetDateTime.toLocalDate();
LocalTime localTime =
offsetDateTime.toLocalTime();
!
System.out.println(offsetDateTime);
System.out.println(localDate);
System.out.println(localTime);
2014-03-21T15:30:45+09:00
2014-03-21
15:30:45
LocalDate commonYearsDay
= LocalDate.of(1990, 12, 19);
LocalDate leapYearsDay
= LocalDate.of(1992, 10, 8);
LocalDate centuryDay
= LocalDate.of(2000, 1, 1);
!
System.out.println(
commonYearDay.isLeapYear());
System.out.println(
leapYearDay.isLeap());
System.out.println(
centuryDay.isLeapYear());
false
true
false
LocalDate today
= LocalDate.of(2014, 3, 21);
LocalDate lastWeek
= today.minusWeeks(1L);
LocalDate dayAfterTomorrow
= today.plusDays(2L);
Period period =
lastWeek.until(dayAfterTomorrow);
!
System.out.println(yesterday);
System.out.println(dayAftereTomorrow);
System.out.println(period);
2014-03-20
2014-03-23
P9D
// Convert java.util.Date to LocalDateTime
Date date = new Date();
!
LocalDateTime localDateTime =
LocalDateTime.ofInstant(date.toInstant(),
ZoneId.systemDefault());
// Convert LocalDateTime to java.util.Date
LocalDateTime localDateTime =
LocalDateTime.now();
!
Instant instant = localDateTime.atZone(
ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
Brand new
Date and Time API
March 21, 2014
HASUNUMA Kenji

khasunuma@java.net

http://www.coppermine.jp/

Twitter: @khasunuma

More Related Content

What's hot

What Year Is It: things you shouldn't do with timezones
What Year Is It: things you shouldn't do with timezonesWhat Year Is It: things you shouldn't do with timezones
What Year Is It: things you shouldn't do with timezones
Aram Dulyan
 
Bit calculations
Bit calculationsBit calculations
Bit calculations
alc1411
 
Building Structure calculation
Building Structure calculationBuilding Structure calculation
Building Structure calculation
Jy Chong
 
Variable mass system
Variable mass systemVariable mass system
Variable mass systempastimespace
 
Ejercicio de mecanica
Ejercicio de mecanicaEjercicio de mecanica
Ejercicio de mecanica
Josè Colmenarez Cordero
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heaps
Vivek Bhargav
 
Multi-Jet Generation -status report-
Multi-Jet Generation -status report-Multi-Jet Generation -status report-
Multi-Jet Generation -status report-
Yoshitaro Takaesu
 
5th heat transfer class, tuesday, january 28, 2014
5th heat transfer class, tuesday, january 28, 20145th heat transfer class, tuesday, january 28, 2014
5th heat transfer class, tuesday, january 28, 2014
Raymond Adamic
 

What's hot (9)

What Year Is It: things you shouldn't do with timezones
What Year Is It: things you shouldn't do with timezonesWhat Year Is It: things you shouldn't do with timezones
What Year Is It: things you shouldn't do with timezones
 
Sol59
Sol59Sol59
Sol59
 
Bit calculations
Bit calculationsBit calculations
Bit calculations
 
Building Structure calculation
Building Structure calculationBuilding Structure calculation
Building Structure calculation
 
Variable mass system
Variable mass systemVariable mass system
Variable mass system
 
Ejercicio de mecanica
Ejercicio de mecanicaEjercicio de mecanica
Ejercicio de mecanica
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heaps
 
Multi-Jet Generation -status report-
Multi-Jet Generation -status report-Multi-Jet Generation -status report-
Multi-Jet Generation -status report-
 
5th heat transfer class, tuesday, january 28, 2014
5th heat transfer class, tuesday, january 28, 20145th heat transfer class, tuesday, january 28, 2014
5th heat transfer class, tuesday, january 28, 2014
 

Similar to Brand New Date and Time API

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
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulationShahjahan Samoon
 
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
 
Data Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing ItData Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing It
kanaugust
 
jkfdlsajfklafj
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
PlanetExpressATX
 
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
 
MomentJS at SeattleJS
MomentJS at SeattleJSMomentJS at SeattleJS
MomentJS at SeattleJS
Maggie Pint
 
Date and Time MomentJS Edition
Date and Time MomentJS EditionDate and Time MomentJS Edition
Date and Time MomentJS Edition
Maggie Pint
 
That Conference Date and Time
That Conference Date and TimeThat Conference Date and Time
That Conference Date and Time
Maggie Pint
 
Fun times with ruby
Fun times with rubyFun times with ruby
Fun times with ruby
Geoff Harcourt
 
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
PeckaDesign.cz
 
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
 
Date and Time Odds Ends Oddities
Date and Time Odds Ends OdditiesDate and Time Odds Ends Oddities
Date and Time Odds Ends Oddities
Maggie Pint
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
Rasheed Waraich
 
Php date &amp; time functions
Php date &amp; time functionsPhp date &amp; time functions
Php date &amp; time functions
Programmer Blog
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
Amrit Kaur
 
Advanced Date/Time Handling with PHP
Advanced Date/Time Handling with PHPAdvanced Date/Time Handling with PHP
Advanced Date/Time Handling with PHPAnis Berejeb
 

Similar to Brand New Date and Time API (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
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulation
 
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
 
Data Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing ItData Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing It
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
doc
docdoc
doc
 
jkfdlsajfklafj
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
 
Java 8 Date and Time API
Java 8 Date and Time APIJava 8 Date and Time API
Java 8 Date and Time API
 
MomentJS at SeattleJS
MomentJS at SeattleJSMomentJS at SeattleJS
MomentJS at SeattleJS
 
Date and Time MomentJS Edition
Date and Time MomentJS EditionDate and Time MomentJS Edition
Date and Time MomentJS Edition
 
That Conference Date and Time
That Conference Date and TimeThat Conference Date and Time
That Conference Date and Time
 
Fun times with ruby
Fun times with rubyFun times with ruby
Fun times with ruby
 
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
Péhápkaři v Pecce: Za hranicemi DateTime – Jiří Pudil – 16. 10. 2019
 
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
 
Date and Time Odds Ends Oddities
Date and Time Odds Ends OdditiesDate and Time Odds Ends Oddities
Date and Time Odds Ends Oddities
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
 
Php date &amp; time functions
Php date &amp; time functionsPhp date &amp; time functions
Php date &amp; time functions
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
 
Advanced Date/Time Handling with PHP
Advanced Date/Time Handling with PHPAdvanced Date/Time Handling with PHP
Advanced Date/Time Handling with PHP
 

More from 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
 
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
 
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
 
Basic method for Java EE Web Profile
Basic method for Java EE Web ProfileBasic method for Java EE Web Profile
Basic method for Java EE Web Profile
Kenji HASUNUMA
 
Introduction to JavaFX Dialogs
Introduction to JavaFX DialogsIntroduction to JavaFX Dialogs
Introduction to JavaFX Dialogs
Kenji HASUNUMA
 
Virtualization Fundamental
Virtualization FundamentalVirtualization Fundamental
Virtualization Fundamental
Kenji HASUNUMA
 
JLS Myths - If-then-else statement -
JLS Myths - If-then-else statement -JLS Myths - If-then-else statement -
JLS Myths - If-then-else statement -
Kenji HASUNUMA
 
Fundamental Java
Fundamental JavaFundamental Java
Fundamental Java
Kenji HASUNUMA
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
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 Date and Time API
Introduction to Date and Time APIIntroduction to Date and Time API
Introduction to Date and Time API
Kenji HASUNUMA
 

More from Kenji HASUNUMA (13)

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
 
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
 
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
 
Basic method for Java EE Web Profile
Basic method for Java EE Web ProfileBasic method for Java EE Web Profile
Basic method for Java EE Web Profile
 
Introduction to JavaFX Dialogs
Introduction to JavaFX DialogsIntroduction to JavaFX Dialogs
Introduction to JavaFX Dialogs
 
Virtualization Fundamental
Virtualization FundamentalVirtualization Fundamental
Virtualization Fundamental
 
JLS Myths - If-then-else statement -
JLS Myths - If-then-else statement -JLS Myths - If-then-else statement -
JLS Myths - If-then-else statement -
 
Fundamental Java
Fundamental JavaFundamental Java
Fundamental Java
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
 
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 Date and Time API
Introduction to Date and Time APIIntroduction to Date and Time API
Introduction to Date and Time API
 

Recently uploaded

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
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
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
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
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
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
 
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
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
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
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
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
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
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
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
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
 
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...
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
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
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 

Brand New Date and Time API

  • 1. Brand new Date and Time API March 21, 2014 HASUNUMA Kenji khasunuma@java.net http://www.coppermine.jp/ Twitter:
  • 2. Preface • JSR 310 is a new and improved date and time API for Java. • It is based on ISO 8601 standard. • The reference implementation is called "ThreeTen". • Specification Leads: Stephen Colebourne, Michael Nascimento Santos and Roger Riggs.
  • 3. What is time? “Time” is described by science approaches and defined by International Standard (SI) exactly. Skip
  • 4.
  • 5. Definition of second (Traditional)
  • 7.
  • 8.
  • 9.
  • 10. PST (Pacific Standard Time) Offset:-08:00 (Summer:-07:00) JST (Japan Standard Time) Offset:+09:00 Skip
  • 11. Time zones (East 1/2) Code Name Offset Summer ECT Europe/Paris +01:00 +02:00 CAT Africa/Harare +02:00 (N/A) ART Africa/Cairo +02:00 (N/A) EAT Africa/Addis_Abeba +03:00 (N/A) NET Asia/Yerevan +04:00 (N/A) PLT Asia/Karachi +05:00 (N/A) IST Asia/Koltaka +05:30 (N/A) BST Asia/Dacca +06:00 (N/A)
  • 12. Time zones (East 2/2) Code Name Offset Summer VST Asia/Ho_Chi_Minh +07:00 (N/A) CTT Asia/Shanghai +08:00 (N/A) JST Asia/Tokyo +09:00 (N/A) ACT Australia/Darwin +09:30 (N/A) AET Australia/Sydney +11:00 +10:00 SST Pacific/Guadalcanal +11:00 (N/A) NST Pacific/Auckland +13:00 +12:00 MIT Pacific/Apia +14:00 +13:00
  • 13. Time zones (West 1/2) Code Name Offset Summer BET America/Sao_Paulo -02:00 -03:00 AGT America/Argentina/Buenos_Aires -03:00 (N/A) CNT America/St_Johns -03:30 -02:30 PRT America/Puerto_Rico -04:00 (N/A) EST (N/A) -05:00 (N/A) IET America/Indiana/Indianapolis -05:00 -04:00
  • 14. Time zones (West 2/2) Code Name Offset Summer CST America/Chicago -06:00 -05:00 MST (N/A) -07:00 (N/A) PNT America/Phoenix -07:00 (N/A) PST America/Los_Angeles -08:00 -07:00 AST America/Anchorage -09:00 -08:00 HST (N/A) -10:00 (N/A)
  • 15. ISO 8601 Date elements and interchange formats - Information interchange - Representation of dates and times Skip
  • 16. What’s ISO 8601? • International standard for representation of dates and times, using information interchange. • Based on Gregorian calendar system (1582). • JIS X 0301 is Japanese translations with remarks about Japanese chronology. • ISO 8601 and Unix time are incompatible.
 Unix time is basis of java.util.Date, et al.
  • 17. Representation of time (w/o Time zone) • hh:mm:ss e.g. 15:30:45 • hh:mm e.g. 15:30 • hh e.g. 15 • hh:mm:ss.s e.g. 15:30:45.250
  • 18. Representation of time (w/Time zone) • Add suffix based on the offset from UTC • UTC: ‘Z’ e.g. 06:30:45Z • Not UTC: +hh:mm or -hh:mm
 e.g. 15:30:45+09:00 (JST, Asia/Tokyo)
 02:30:45-08:00 (PST, America/Los_Angeles)
  • 19. Representation of date • date - it consists with year, month, day and week. • calendar date YYYY-MM-DD e.g. 2014-03-21 • ordinal date YYYY-DDD e.g. 2014-080 • week date YYYY-Www-D e.g. 2014-W12-5 • Short representations: • year-month YYYY-MM e.g. 2014-03 • year YYYY e.g. 2014 • century YY e.g. 20 • month-day --MM-DD e.g. --03-21
  • 20. Definition of day • a day = 24 hours = 86,400 seconds • 00:00~24:00 (24:00 is same as next 00:00) • Exceptions: • Leap seconds • Begin and end of summer time • When the time zone is changed
  • 21. Definition of month • a month = 28, 29, 30 or 31 days 1 January 01~31 7 July 01~31 2 February 01~28/29 8 August 01~31 3 March 01~31 9 September 01~30 4 April 01~30 10 October 01~31 5 May 01~31 11 November 01~30 6 June 01~30 12 December 01~31
  • 22. Definition of year • a year = approximation days while the orbital period of Earth (365.242 19 days), 12 months. • common year = 365 days • leap year = 366 days • 0000~9999 (0000~1582 is reserved) mod 4 mod 100 mod 400 year Not Zero - - common year Zero Not Zero - leap year Zero Zero Not Zero common year Zero Zero Zero leap year
  • 23. Definition of week • a week = 7 days • 1st week of year = a week contents 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
  • 25. Representation of date and time • concat date and time by 'T'. • YYYY-MM-DDThh:mm:ss[Time zone] • Alt. YYYY-DDDThh:mm:ss[Time Zone] • e.g. 2014-03-21T06:30 2014-03-21T15:30:45+09:00 2014-079T02:30:45-08:00
  • 26. Definition and representation of duration • time amount between two time points • date : nYnMnD e.g. 1Y3M22D • time : nHnMnS e.g. 9H30M45S • date and time : nYnMnDTnHnMnS
 e.g. 1Y3M22DT9H30M45S
  • 27. Definition and representation of period • range between two dates (adopt also two times) • two dates : YYYY-MM-DD/YYYY-MM-DD
 e.g. 2013-12-31/2014-03-21 • start date and duration : YYYY-MM-DD/PnYnMnD
 e.g. 2013-12-31/P1Y3M22D • end date and duration : PnYnMnD/YYYY-MM-DD
 e.g. P1Y3M22D/2013-03-21 • duration : PnYnMnD
 e.g. P1Y3M22D
  • 28. JSR 310 Date and Time API Skip
  • 29. JSR 310 : Date and Time API • Since 2007, Reference Implementation: ThreeTen • Relational projects: • ThreeTen-Extra: Features dropped from JDK8, 
 i.e. UTC/TAI full support, Coptic chronology, et al.
 https://github.com/ThreeTen/threeten-extra • ThreeTen-Backport: JSR 310 like API for JDK7.
 https://github.com/ThreeTen/threetenbp • Joda-Time: Provides many hints to JSR 310.
 http://www.joda.org/joda-time/
  • 30. Package java.time.* Package Description Use java.time Basic classes usually java.time.format Date and Time Formatter often java.time.chrono Chronology supports partially java.time.temporal Low-level API rarely java.time.zone Low-level API rarely
  • 31. Date and Time classes Class Field Time zone ISO 8601 Chrono. LocalDate Date N/A compatible enable LocalDateTime Date + Time N/A compatible enable LocalTime Time N/A compatible N/A OffsetDateTime Date + Time offset compatible disable OffsetTime Time offset compatible N/A ZonedDateTime Date + Time zone id extended enable
  • 32. Date and Time conversions
  • 33. Date and Time conversions Oper. Description of- Create from fields. e.g. LocalDate today = LocalDate.of(2014, 3, 21); // today.toString() : “2014-03-21” at- Expand with fields. e.g. LocalDateTime current = today.atTime(15:30); // current.toString() : “2014-03-21T15:30:00” to- Truncate fields. e.g. LocalDate someday = current.toLocalDate(); // someday.toString() : “2014-03-21”
  • 34. Date and Time operators Oper. Description isBefore isEqual isAfter Compare with other date/time. e.g. LocalDate today = LocalDate.of(2014, 3, 21); boolean b = today.isAfter(LocalDate.of(2014, 3, 19)); // b == true plus- minus- Add/subtract field value. e.g. LocalDate tomorrow = today.plusDays(2); // tomorrow.toString() : “2014-03-23” isLeapYear Verify a date is leap year. e.g. boolean leap = today.isLeapYear(); // leap == false
  • 35. Date and Time formatting Method Description toString() Format using default formatter. e.g. String s = today.toString(); // s : “2014-03-21” format(DateTimeFormatter f) Format using custom formatter. e.g. String s = today.format(formatter); parse(String s) Parse using default formatter. e.g. LocalDate d = LocalDate
 .parse(“2014-03-21”); parse(String s, DateTimeFormatter f) Parse using custom formatter. e.g. LocalDate d = LocalDate
 .parse(“2014-03-21”, formatter);
  • 36. Chronology • Date and Time API supports ISO 8601 and some local chronology (e.g., Japanese Era, Minguo Era, Thai Buddhist Era and Hijrah Era). • ChronoLocalDate and its sub-classes (incl. LocalDate) support chronology. • ChronoLocalDateTime<D>/ChronoZonedDateTime<D> instead of LocalDateTime/ZonedDateTime • dates/times that have different chronology can convert by from method each other.
  • 37. Chronology supports Chronology Era ChronoLocalDate IsoChronology IsoEra LocalDate JapaneseChronology JapaneseEra JapaneseDate MinguoChronology MinguoEra MinguoDate ThaiBuddhistChronology ThaiBuddhistEra ThaiBuddhistDate HijrahChronology HijrahEra HijrahDate
  • 38. Chronology examples LocalDate today = LocalDate.of(2014, 3, 21); System.out.println(today); System.out.println(JapaneseDate.from(today)); System.out.println(MinguoDate.from(today)); System.out.println(ThaiBuddhistDate.from(today)); System.out.println(HijrahDate.from(today)); 2014-03-21 Japanese Heisei 26-03-21 Minguo ROC 103-03-21 ThaiBuddhist BE 2557-03-21 Hijrah-umalqura AH 1435-05-20
  • 39. DateTimeFormatter • Provides formats date/time. • Created by ofPattern method (basic way)
 e.g. DateTimeFormatter.ofPattern(“yyyy/MM/dd”) • Created by DateTimeFormatterBuilder (full control) • Pre-defined patterns (static fields) • ISO_LOCAL_DATE // ofPattern(“yyyy-MM-dd”) • ISO_ORDINAL_DATE // ofPattern(“yyyy-ddd”) • ISO_WEEK_DATE // ofPattern(“yyyy-Www”)
  • 40. Chronology and Formatter e.g. JapaneseDate pattern display example "YYYY/MM/dd" 2014/03/21 "Y/M/d" 2014/3/21 "GGGGGyy.MM.dd" H26.03.21 "Gy年M月d日(E)" 平成26年3月21日(金)
  • 43. Instant • Representation of a time-point. • The precision is a nano second. • The epoch is 1970-01-01T00:00:00Z • Convert from/to LocalDateTime, OffsetDateTime or ZonedDateTime. • Convert from/to java.util.Date, which means that Instant is the only interface to java.util.Date.
  • 44. Duration and Period • Representations of period (ISO 8601). • Period (JSR 310) is a period (ISO 8601) between two dates and represents as a date-scale.
 i.e. format as "P1Y2M3D" • Duration (JSR 310) is a period (ISO 8601) between any two temporals and represents as a time-scale.
 i.e. format as "PT15H30M45D" • Attention: those definitions are different from them of ISO 8601.
  • 45. Clock and now method • Provider of the current instant. • There are some clocks, having relation of zones (incl. current zone), fixed clock (for testing) and custom. • By default, it uses the clock of current zone. • now method (LocalDate, et al.) creates a temporal instance from a clock. 
 e.g. LocalDate.now(); 
 LocalDate.now(zoneId); 
 LocalDate.now(Clock.fixed(instant, ZoneId.systemDefault);
  • 47. // It is assumed to run at March 21, 2014 LocalDate today = LocalDate.new(); ! System.out.println(today); 2014-03-21
  • 48. LocalDate calendarDate = LocalDate.of(2014, 3, 21); LocalDate ordinalDate = LocalDate.ofYearDays(2014, 80); LocalDateTime localDateTime = LocalDateTime.of(2014, 3, 21, 15, 30); ! System.out.println(calendarDate); System.out.println(ordinalDate); System.out.println(localDateTime); 2014-03-21 2014-03-21 2014-03-21T15:30
  • 49. OffsetDateTime offsetDateTime = OffsetDateTime.of(2014, 3, 21, 15, 30, 45, 0, ZoneOffset.ofHours(9)); ZonedDateTime zonedDateTime = ZonedDateTime.of(2014, 3, 21, 15, 30, 45, 0, ZoneId.of("Asia/Tokyo")); ! System.out.println(offsetDateTime); System.out.println(zonedDateTime); 2014-03-21T15:30:45+09:00 2014-03-21T15:30:45+09:00[Asia/Tokyo]
  • 50. LocalDate localDate = LocalDate.of(2014, 3, 21); LocalDateTime localDateTime0 = localDate.atStartOfDay(); LocalDateTime localDateTime1 = localDate.atTime(15, 30, 45); OffsetDateTime offsetDateTime = localDateTime.atOffset( ZoneOffset.ofHours(9)); ! System.out.println(localDateTime0); System.out.println(localDateTime1); System.out.println(offsetDateTime); 2014-03-21T00:00 2014-03-21T15:30:45 2014-03-21T15:30:45+09:00
  • 51. OffsetDateTime offsetDateTime = OffsetDateTime.of(2014, 3, 21, 15, 30, 45, 0, ZoneOffset.ofHours(9)); LocalDate localDate = offsetDateTime.toLocalDate(); LocalTime localTime = offsetDateTime.toLocalTime(); ! System.out.println(offsetDateTime); System.out.println(localDate); System.out.println(localTime); 2014-03-21T15:30:45+09:00 2014-03-21 15:30:45
  • 52. LocalDate commonYearsDay = LocalDate.of(1990, 12, 19); LocalDate leapYearsDay = LocalDate.of(1992, 10, 8); LocalDate centuryDay = LocalDate.of(2000, 1, 1); ! System.out.println( commonYearDay.isLeapYear()); System.out.println( leapYearDay.isLeap()); System.out.println( centuryDay.isLeapYear()); false true false
  • 53. LocalDate today = LocalDate.of(2014, 3, 21); LocalDate lastWeek = today.minusWeeks(1L); LocalDate dayAfterTomorrow = today.plusDays(2L); Period period = lastWeek.until(dayAfterTomorrow); ! System.out.println(yesterday); System.out.println(dayAftereTomorrow); System.out.println(period); 2014-03-20 2014-03-23 P9D
  • 54. // Convert java.util.Date to LocalDateTime Date date = new Date(); ! LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); // Convert LocalDateTime to java.util.Date LocalDateTime localDateTime = LocalDateTime.now(); ! Instant instant = localDateTime.atZone( ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant);
  • 55. Brand new Date and Time API March 21, 2014 HASUNUMA Kenji khasunuma@java.net http://www.coppermine.jp/ Twitter: @khasunuma