Part 8 - Date - Time in Java 8
Part 8 - Date - Time in Java 8
Richard Warburton
Raoul-Gabriel Urma
Outline
• Mutability
• No Timezones
Calendar
• Still mutable
TimeZone zone =
TimeZone.getTimeZone("Asia/HongKong");
TimeZone zone =
TimeZone.getTimeZone("Asia/Hong_Kong");
• Instant
• Precise moment in time
• Milliseconds since epoch
• Duration
• Time in milliseconds between two points.
New Concepts
• Period
• A period of time defined in terms of
human readable fields, eg 1 Month
• Chronology
• A calculation engine supporting rules of a
specific calendar
Problems
• Nulls
• Joda Time treats nulls as zero
• Can lead to unexpected results from bugs
• Easy to miss a problem
• Pluggable Chronologies
• Powerful for multiple calendaring systems
• How do you know what you are using?
• How do you extend?
Problems
• Human/Machine Timeline
• Machine counts from 0
• Humans have logical dates
• Internal Implementation
• Could be improved
• Weak Typing
• Lots of constructors take Object
Quiz
• java.time
• The core classes and most frequently used
• java.time.chrono
• Alternative calendaring systems
• java.time.format
• Formatting and parsing tools
java.time overview
• java.time.temporal
• Interfaces required for core classes
• java.time.zone
• Underlying time zone rules, not often used
Zone Id vs Offset
• java.time.ZoneId
• Identifier for a time zone
• CEST or Europe/Amsterdam
• Can resolve to an offset at a point in time
• java.time.ZoneOffset
• Duration of time away from UTC
• Many-to-many relationship with Ids
Duration vs Period
• Duration
• Time based distance on the timeline
• eg: 2 seconds + 3 nanoseconds
• Period
• Date based distance on the timeline
• eg: 3 weeks + 2 days
• Immutable implementations
DATE LocalDate
TIME LocalTime
TIMESTAMP LocalDateTime
TIME WITH OffsetTime
TIMEZONE
TIMESTAMP WITH OffsetDateTime
TIMEZONE
Live Coding: TripExample
Summary
Summary
com.java_8_training.problems.datetime.TestBirthdayDiary
BirthdayDiary