Java 8 Date Time
By Jim Gough

@JavaJimLondon
Agenda
•
•
•
•
•
•
•

Adopt a JSR

What exists in Java 7

New Things - The Basics

Working with Date and Time

Parsing and Formatting

Interoperability with java.util.Date

Advanced Queries
!2
Accompanying Materials
• Code along with github project:

• https:/
/github.com/jpgough/JavaTimeLab

!

• More information on my blog:

• http:/
/javajimlondon.blogspot.com/2014/01/
live-coding-on-java-8-date-time-api.html

!3
LJC/JCP
•

LJC elected onto the JCP committee


•

JCP (Java Community Process)

• The JCP is the mechanism for developing standard
technical specifications for the Java technology

!4
Adopt a JSR
•

This is it! JSR-310 is the pilot 


•

This program is intended to:

– Encourage members of the Java Community to get
involved in a JSR.

– Evangelise that JSR to the wider Java Community
community in order to increase grass roots
participation

– https:/
/java.net/projects/adoptajsr/pages/Home
Java Date
•

Java Date has been in the language since January 23,
1996


•

Many good discussions about what’s wrong with dates 


•

Mutability


•

Date is a DateTime, but there are other classes for SQL


•

No Timezones


•

Not easy to use

!6
Calendar
• Still mutable

• Can’t format a date directly

• Performing arithmetic operations on

dates not clearly supported. For
example time between two points

!7
Example From It’s High Time
@JavaOne 2008

•

How many bugs in this code?




Date date = new Date(2007, 12, 13, 16,
40); 




TimeZone zone =
TimeZone.getTimeZone("Asia/HongKong");




Calendar cal = new
GregorianCalendar(date, zone); 

DateFormat fm = new
SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);
!8
Example From It’s High Time
@JavaOne 2008

•

6 bugs in the code!




Date date = new Date(2007, 12, 13, 16,
40); 




TimeZone zone =
TimeZone.getTimeZone("Asia/Hong_Kong");




Calendar cal = new
GregorianCalendar(date, zone); 

DateFormat fm = new
SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);
!9
New Things - The Basics
•

New package java.time


•

New objects for representing Dates and Time

– LocalDate

– LocalTime

– LocalDateTime

– ZonedDateTime

!10
Working with Date and Time
•

Instant - Closest thing to java.util.Date


•

Duration

– Measure of time. Eg 34.5 seconds


•

Period

– A date based amount of time. Eg 5 days


•

Difference important when working with
ZonedDateTime operations.

!11
Java.util.Date
•

java.util.Date is actually closest to Instant


•

Now has to toInstant method on


•

Examples

!12
Parsing and Formatting
•

DateTimeFormatter


•

Nice predefined formatters:

– DateTimeFormatter.ISO_DATE


•

Can build a custom pattern

– Some slight modification to patterns


•

.format and .parse conveniently on objects.

!13

Introduction to Java 8 java.time

  • 1.
    Java 8 DateTime By Jim Gough @JavaJimLondon
  • 2.
    Agenda • • • • • • • Adopt a JSR Whatexists in Java 7 New Things - The Basics Working with Date and Time Parsing and Formatting Interoperability with java.util.Date Advanced Queries !2
  • 3.
    Accompanying Materials • Codealong with github project: • https:/ /github.com/jpgough/JavaTimeLab ! • More information on my blog: • http:/ /javajimlondon.blogspot.com/2014/01/ live-coding-on-java-8-date-time-api.html !3
  • 4.
    LJC/JCP • LJC elected ontothe JCP committee • JCP (Java Community Process) • The JCP is the mechanism for developing standard technical specifications for the Java technology !4
  • 5.
    Adopt a JSR • Thisis it! JSR-310 is the pilot • This program is intended to: – Encourage members of the Java Community to get involved in a JSR. – Evangelise that JSR to the wider Java Community community in order to increase grass roots participation – https:/ /java.net/projects/adoptajsr/pages/Home
  • 6.
    Java Date • Java Datehas been in the language since January 23, 1996 • Many good discussions about what’s wrong with dates • Mutability • Date is a DateTime, but there are other classes for SQL • No Timezones • Not easy to use !6
  • 7.
    Calendar • Still mutable •Can’t format a date directly • Performing arithmetic operations on dates not clearly supported. For example time between two points !7
  • 8.
    Example From It’sHigh Time @JavaOne 2008 • How many bugs in this code?
 
 Date date = new Date(2007, 12, 13, 16, 40); 
 
 TimeZone zone = TimeZone.getTimeZone("Asia/HongKong");
 
 Calendar cal = new GregorianCalendar(date, zone); 
 DateFormat fm = new SimpleDateFormat("HH:mm Z");
 String str = fm.format(cal); !8
  • 9.
    Example From It’sHigh Time @JavaOne 2008 • 6 bugs in the code!
 
 Date date = new Date(2007, 12, 13, 16, 40); 
 
 TimeZone zone = TimeZone.getTimeZone("Asia/Hong_Kong");
 
 Calendar cal = new GregorianCalendar(date, zone); 
 DateFormat fm = new SimpleDateFormat("HH:mm Z");
 String str = fm.format(cal); !9
  • 10.
    New Things -The Basics • New package java.time • New objects for representing Dates and Time – LocalDate – LocalTime – LocalDateTime – ZonedDateTime !10
  • 11.
    Working with Dateand Time • Instant - Closest thing to java.util.Date • Duration – Measure of time. Eg 34.5 seconds • Period – A date based amount of time. Eg 5 days • Difference important when working with ZonedDateTime operations. !11
  • 12.
    Java.util.Date • java.util.Date is actuallyclosest to Instant • Now has to toInstant method on • Examples !12
  • 13.
    Parsing and Formatting • DateTimeFormatter • Nicepredefined formatters: – DateTimeFormatter.ISO_DATE • Can build a custom pattern – Some slight modification to patterns • .format and .parse conveniently on objects. !13