Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Java 8 java.time

5,582 views

Published on

A code along session to introduce the java.time library in the upcoming release of Java 8. The materials to code along can be cloned from github here: https://github.com/jpgough/JavaTimeLab

Published in: Technology, Education
  • Be the first to comment

Introduction to Java 8 java.time

  1. 1. Java 8 Date Time By Jim Gough @JavaJimLondon
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 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. 8. 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
  9. 9. 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
  10. 10. New Things - The Basics • New package java.time • New objects for representing Dates and Time – LocalDate – LocalTime – LocalDateTime – ZonedDateTime !10
  11. 11. 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
  12. 12. Java.util.Date • java.util.Date is actually closest to Instant • Now has to toInstant method on • Examples !12
  13. 13. 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

×