SlideShare a Scribd company logo
Joda-Time & JSR 310
– Problems, Concepts and Approaches
Justin Lin
caterpillar@openhome.cc
http://openhome.cc
Agenda
• Date and Calendar Problems
• Time ABC
• Joda-Time
• JSR310
2 / 66
3 / 66
What's wrong?
• I am 818 years old?
Rounding off error
4 / 66
Fixed
• I am 38 years old.
There's a long type. All other
operands are promoted.
5 / 66
• Taiwan Java Developer Day is 1913/9/2?
What's wrong?
Deprecated
6 / 66
• Taiwan Java Developer Day is 2013/9/2?
What's wrong?
This calendar field
begins from 0.
7 / 66
• Taiwan Java Developer Day is 2013/8/2.
Fixed
Use the constant
variable of Calendar.
8 / 66
• Days between two calendars is zero?
What's wrong?
The Calendar instance is
mutable and state-reserved.
9 / 66
0
Fixed
clone() the instance , or
create a new instance.
10 / 66
The Date instance is not a date
• It represents a specific instant in time, with
millisecond precision.
– For example, 1375430498832 milliseconds after
"the epoch", namely January 1, 1970, 00:00:00
UTC.
11 / 66
• Prior to JDK 1.1, It allowed the interpretation
of dates as year, month, day, hour, minute,
and second values. But…wired values…
– A year y is represented by the integer y - 1900.
– A month, hour, minutes and second begin from 0.
– A day of month begins from 1.
12 / 66
• All those methods about the interpretation of
dates are deprecated after JDK 1.1.
• The setTime method is not deprecated. The
Date instance is mutable.
• Converting between a specific instant in time
manually is error-prone. The Calendar class
provides methods for that. But …
The Date instance is not a specific instant
13 / 66
• A set of calendar fields such as YEAR, MONTH,
DAY_OF_MONTH, HOUR, and so on.
– A month, hour, minute and second begin from 0.
– Remember to use Calendar.JANUARY.
• The Calendar instance is mutable. Methods
such as add, set and roll changes the state
of the instance.
Using Calendar can be painful and tedious
14 / 66
15 / 66
Time the time
• Greenwich Mean Time(GMT)
– Originally referred to the mean solar time at the Royal
Observatory in Greenwich, London.
– Noon Greenwich Mean Time is the moment when the
sun reaches its highest point in the sky.
– GMT is sometimes used loosely and arguably as a
synonym for UTC.
• Universal Time(UT)
– Observe stars as they crossed a meridian.
– In 1935, the term Universal Time was recommended as
a more precise term than Greenwich Mean Time.
– GMT is the same as UT before 1972.
16 / 66
• International Atomic Time(TAI)
– The SI( International System of Units) second was
defined in terms of the caesium atom in 1967.
– Synchronised with Universal Time at the beginning of
1958.
• Coordinated Universal Time(UTC)
– Based on TAI. Introduced on at the beginning 1972.
– With leap seconds added at irregular intervals to
compensate for the slowing of Earth's rotation.
– Since 30 June 2012 when the last leap second was
added, TAI has been exactly 35 seconds ahead of UTC.
17 / 66
• Unix time
– A system for describing instants in time.
– Defined as the number of seconds that have
elapsed since 00:00:00 UTC on 1 January 1970,
not counting leap seconds.
• The epoch(date)
– An instant in time chosen as the origin of a
particular era.
– The Unix epoch is the time 00:00:00 UTC on 1
January 1970.
18 / 66
Chronologies
• Julian calendar
– A reform of the Roman calendar introduced by Julius
Caesar in 46 BC, took effect in 45 BC, and widely used
from about 4CE to 1582CE.
– Defines a leap year as once every four years.
• Gregorian calendar
– A reform of the Julian calendar.
– The last day of the Julian calendar was Thursday, 4
October 1582.
– Friday, 15 October 1582 was the first day of the
Gregorian calendar.
19 / 66
• Gregorian calendar (continued)
– Britain and the British Empire (including the
eastern part of what is now the United States)
adopted the Gregorian calendar in 1752.
20 / 66
• Gregorian calendar (continued)
– The concrete subclass GregorianCalendar of
Calendar is a hybrid calendar that supports
both the Julian and Gregorian calendar systems.
– setGregorianChange sets the Gregorian
Calendar change date. Default is October 15, 1582
(Gregorian).
21 / 66
Fri Oct 15 1582
Thu Oct 4 1582
• The ISO8601 standard
– An international standard covering the exchange of
date and time-related data.
– Provide an unambiguous and well-defined method
of representing dates and times.
• yyyy-mm-ddTHH:MM:SS.SSS
• yyyy-dddTHH:MM:SS.SSS
• yyyy-Www-dTHH:MM:SS.SSS
• ...
22 / 66
• The ISO8601 standard (continued)
– When representing dates and times, the Gregorian
and ISO8601 differ slightly.
– In ISO8601, "19" refer to the century from 1900 to
1999 inclusive.
– In Gregorian, the 19th century is 1801 to 1900
inclusive.
From: https://en.wikipedia.org/wiki/ISO_8601
23 / 66
Time Zones
• A region that has a uniform standard time for
legal, commercial, social, and political purposes.
• Most of the time zones on land are offset from
UTC (UTC−12 to UTC+12)
– A time change of one hour is required for each
change of longitude by 15°.
– The UTC time zone is sometimes denoted by Z.
24 / 66
• Some countries, such as China and India, use a
single time zone.
https://upload.wikimedia.org/wikipedia/commons/a/ad/Standard_time_zones_of_the_world.png
25 / 66
• Some higher latitude countries use daylight
saving time (summer time) for part of the
year, typically by changing clocks by an hour.
• Taiwan DST were from 1945 to 1961 and 1974
to 1975.
26 / 66
• JDK time zone data is updated when the JDK is
updated.
• Timezone Updater Tool (aka TZUpdater)
– http://www.oracle.com/technetwork/java/javase/
downloads/tzupdater-download-513681.html
27 / 66
Joda-Time
Joda-Time
• Created in 2002.
• Released as v1.0 in 2005.
• Released as v2.0 In 2011.
• Currently v2.2.
Stephen Colebourne
29 / 66
Key concepts
• Instant
– Defined as an instant in the datetime continuum specified
as a number of milliseconds from 1970-01-01T00:00Z.
• The ReadableInstant defines an instant in the
datetime continuum.
– Instant
– DateTime
– DateMidnight
– MutableDateTime
30 / 66
Immutable
• Instant (continued)
– The millisecond instant can be converted to any
date time field using a Chronology.
Instant
Time-line
31 / 66
• Partial
– A partial date and time representation.
– May 26 could apply to any year. 13:06 p.m. could apply
to any day of any year.
• The ReadablePartial interface defines a
partial time.
– LocalDate、LocalTime、LocalDateTime
– YearMonth、MonthDay
– Partial
– YearMonthDay
– TimeOfDay
32 / 66
• Partial (continued)
Instant
Time-line
= Partial + missing fields + time zone
33 / 66
• Interval
– An interval of time between two instants.
• Interval is defined by the ReadableInterval
interface.
– Interval
– MutableInterval
34 / 66
• Interval (continued)
Start instant End instant
Interval
35 / 66
• Duration
– Represents a duration of time measured in
milliseconds.
• Duration is represented by the
ReadableDuration interface.
– Duration
instant + Duration = instant
36 / 66
• Period
– Represents the same concept as Duration, but in
"human" terms such as years, months, and weeks.
• Period is represented by the
ReadablePeriod interface.
– Period
– MutablePeriod
– Years、Months、Weeks、Days
– Hours、Minutes、Seconds
37 / 66
• Period (continued)
instant + Period = instant
38 / 66
• Chronology
– A chronology is a pluggable calendar system.
• The Chronology class provides access to the
individual date time fields.
– ISO8601 (Default) - ISOChronology
– Gregorian - GregorianChronology
– GregorianJulian – GJChronology
– Julian - JulianChronology
– Buddhist - BuddhistChronology
– Coptic - CopticChronology
– Ethiopic - EthiopicChronology
– Islamic - IslamicChronology
GregorainCalendar
replacement
39 / 66
• Chronology (continued)
40 / 66
• Time Zone
– Joda-Time also compiles the time zone data into
our jar file.
– You can update the raw data and recompile the jar
at any time.
– http://joda-time.sourceforge.net/tz_update.html
• Available Time Zones
– http://joda-time.sourceforge.net/timezones.html
41 / 66
Beef
• What do you need?
• Period between two Instants or Partials.
42 / 66
• What do you need?
• Most of the time, we need Partial.
43 / 66
• What do you need?
• Period.
44 / 66
• Adding 5 days, 6 months, and 3 weeks to a date
and printing the formatted result.
• Using Joda-Time.
45 / 66
46 / 66
JS R 31
0
Joda-Time as JSR-310?
• Why JSR-310 isn't Joda-Time
– http://blog.joda.org/2009/11/why-jsr-310-isn-
joda-time_4941.html
• Joda-Time has design flaws
– Human/Machine timelines
– Pluggable chronology
– Nulls
– Internal implementation
Stephen Colebourne
47 / 66
Machine/Human timelines
• Machines have one view - a single, ever
increasing number.
• Humans have a totally different view of time.
– Calendar systems.
– Years, months, days, hours, minutes and seconds.
– Time zones
– ...
• In Joda-Time, the DateTime class (human view of an
instant in time) are implementations of
ReadableInstant. This is wrong.
48 / 66
JSR 310
• The java.time package.
• The distinction between computer-related
times and human-related times have been
made more apparent.
49 / 66
Computer-related times
• Instant
– Represents a fixed point in time as an offset from
the standard Java epoch (1st Jan 1970). The
instant is stored to nanosecond resolution.
– Defines its own time-scale, the Java Time-Scale.
– http://download.java.net/jdk8/docs/api/java/time
/Instant.html
50 / 66
Human-related times
• Without a time-zone in the ISO-8601
– LocalDateTime
– LocalDate
– LocalTime
• Years, months, days
– Year, such as 2007.
– YearMonth, such as 2007-12.
– MonthDay, such as 12-03.
51 / 66
An amount of time
• A time-based amount of time.
– Duration, such as '34.5 seconds'.
–Uses nanosecond resolution with a
maximum value of the seconds that can be
held in a long.
• A date-based amount of time
– Period, such as '2 years, 3 months and 4 days'.
52 / 66
Joda-Time vs JSR310
• Joda-Time
• JSR310
53 / 66
• Joda-Time
• JSR310
54 / 66
• Joda-Time
• Using JSR310.
55 / 66
• OffsetDateTime adds to the instant the
offset from UTC in ISO-8601.
• ZonedDateTime adds full time-zone rules
56 / 66
Framework-level API
• The java.time.temporal package.
• TemporalAccessor
– Defines read-only access to a temporal object,
such as a date, time, offset or some combination
of these.
• Temporal extends TemporalAccessor.
– The base interface type for date, time and offset
objects that are complete enough to be
manipulated using plus, minus and with.
57 / 66
• Temporal implementations.
– Instant
– LocalDate、LocalDateTime、LocalTime
– OffsetDateTime、OffsetTime
– Year、YearMonth
– ZonedDateTime
• MonthDay implements TemporalAccessor
rather than Temporal. Why?
– http://download.java.net/jdk8/docs/api/java/time/M
onthDay.html
58 / 66
• plus and minus of Temporal
– plus(TemporalAmount amount)
– plus(long amountToAdd, TemporalUnit unit)
– minus(TemporalAmount amount)
– minus(long amountToSubtract, TemporalUnit unit)
• TemporalAmount defines an amount of
time, such as "6 hours", "8 days" or "2 years
and 3 months".
– Duration
– Period
59 / 66
• TemporalUnit defines a unit of date-time,
such as Days or Hours.
60 / 66
• with of Temporal
– with(TemporalField field, long newValue)
– with(TemporalAdjuster adjuster)
• TemporalField defines a field of date-time, such
as month-of-year or hour-of-minute.
• Use TemporalAdjuster for more complex
alterations
61 / 66
Chronology?
• What is the range of values returned by this
method in Joda-Time?
• The answer is not 1 to 12, but could be 1 to 13!
– The Coptic chronology has 13 months in a year,
and thus can return a range of 1 to 13.
• Most users of the API never check to see if the
chronology is the standard ISO chronology.
62 / 66
• A better solution would be to keep the date/time
classes restricted to a single calendar system.
• Those date and time classes located in the
java.time package are in the ISO-8601 calendar
system.
• Check the java.time.chrono package if you
need other chronologies.
– JapaneseChronology
– ThaiBuddhistChronology
– …
63 / 66
Summery
• Using Date, Calendar and existing date-
related APIs can be error-prone, painful and
tedious.
• The complexities of accurate timekeeping are
beyond your imagine.
• What do you need? Computer-related times
or human-related times?
• Need to manipulate dates and times? Using
Joda-Time or JSR310 to make it easy!
64 / 66
• Other libraries for handling dates and times
– Date4j, Simple Alternative To java.util.Date.
– Arrow, Better dates and times for Python.
– Moment.js, A JavaScript date library for parsing,
validating, manipulating, and formatting dates.
– Noda-Time, A port of Joda-Time to .NET
65 / 66
References
• Joda-Time
– http://joda-time.sourceforge.net/
• Joda-Time - You can't escape time. Why not make it easy?
– http://www.ibm.com/developerworks/java/library/j-
jodatime/index.html
• JSR 310 Date and Time API for Java
– http://www.infoq.com/news/2010/03/jsr-310
• Why JSR-310 isn't Joda-Time
– http://blog.joda.org/2009/11/why-jsr-310-isn-joda-
time_4941.html
• JDK8 Javadoc
– http://download.java.net/jdk8/docs/api/index.html
66 / 66

More Related Content

PPTX
A JSR-310 Date: Beyond JODA Time
PDF
淺談 Groovy 與 Gradle
PDF
Java SE 7 技術手冊投影片第 16 章 - 自訂泛型、列舉與標註
PDF
Java SE 7 技術手冊投影片第 09 章 - Collection與Map
PDF
Java SE 7 技術手冊投影片第 13 章 - 視窗程式設計
PDF
JDK8 Functional API
PDF
Java 開發者的函數式程式設計
PDF
Java SE 7 技術手冊投影片第 10 章 - 輸入輸出
A JSR-310 Date: Beyond JODA Time
淺談 Groovy 與 Gradle
Java SE 7 技術手冊投影片第 16 章 - 自訂泛型、列舉與標註
Java SE 7 技術手冊投影片第 09 章 - Collection與Map
Java SE 7 技術手冊投影片第 13 章 - 視窗程式設計
JDK8 Functional API
Java 開發者的函數式程式設計
Java SE 7 技術手冊投影片第 10 章 - 輸入輸出

Viewers also liked (20)

PDF
Java SE 7 技術手冊投影片第 08 章 - 例外處理
PDF
Java SE 7 技術手冊投影片第 15 章 - 反射器與類別載入器
PDF
Java SE 7 技術手冊投影片第 11 章 - 執行緒與並行API
PDF
Java SE 7 技術手冊投影片第 03 章 - 基礎語法
PPT
Java SE 8 技術手冊第 10 章 - 輸入輸出
PDF
Java SE 7 技術手冊投影片第 07 章 - 介面與多型
PPT
Java SE 8 技術手冊第 4 章 - 認識物件
PPT
Java SE 8 技術手冊第 8 章 - 例外處理
PPT
Java SE 8 技術手冊第 5 章 - 物件封裝
PDF
Spring 2.0 技術手冊目錄
PPT
Java SE 8 技術手冊第 6 章 - 繼承與多型
PDF
Java SE 7 技術手冊投影片第 12 章 - 通用API
PPT
Java SE 8 技術手冊第 9 章 - Collection與Map
PPT
Java SE 8 技術手冊第 1 章 - Java平台概論
PDF
Java SE 7 技術手冊投影片第 04 章 - 認識物件
PDF
Java SE 7 技術手冊投影片第 05 章 - 物件封裝
PDF
Java 8 與 retrolambda
PDF
《Python 3.5 技術手冊》第二章草稿
PDF
java8-patterns
PDF
資料結構
Java SE 7 技術手冊投影片第 08 章 - 例外處理
Java SE 7 技術手冊投影片第 15 章 - 反射器與類別載入器
Java SE 7 技術手冊投影片第 11 章 - 執行緒與並行API
Java SE 7 技術手冊投影片第 03 章 - 基礎語法
Java SE 8 技術手冊第 10 章 - 輸入輸出
Java SE 7 技術手冊投影片第 07 章 - 介面與多型
Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 8 章 - 例外處理
Java SE 8 技術手冊第 5 章 - 物件封裝
Spring 2.0 技術手冊目錄
Java SE 8 技術手冊第 6 章 - 繼承與多型
Java SE 7 技術手冊投影片第 12 章 - 通用API
Java SE 8 技術手冊第 9 章 - Collection與Map
Java SE 8 技術手冊第 1 章 - Java平台概論
Java SE 7 技術手冊投影片第 04 章 - 認識物件
Java SE 7 技術手冊投影片第 05 章 - 物件封裝
Java 8 與 retrolambda
《Python 3.5 技術手冊》第二章草稿
java8-patterns
資料結構
Ad

Similar to Joda-Time & JSR 310 – Problems, Concepts and Approaches (20)

PPTX
Best Practices in Reporting Time Duration in Biometrics
PPTX
Date and Time Odds Ends Oddities
PDF
Geographic Coordinate System.pdf
PPTX
Introduction to Timekeeping
PDF
A brief history of Leap Seconds
PPTX
Time, Why You Punish Me - Cascadia PHP 2024
PPTX
GD-VII-Ch-13-Time-and-Motionnnnnnnnnnn-pdf.pptx
PPTX
Lesson 05 - Time in Distrributed System.pptx
PDF
Java 8 date & time api
PPTX
Class 7 13 time and motion ppt
DOCX
Lab time zones -
DOCX
Lab Time Zones -
PPTX
class7 time motion chapter 8 science ppt.pptx
PPTX
Date and Time MomentJS Edition
PPTX
Unit V Synchronization of distributed system.pptx
PDF
Motion And Time
PPTX
Rotation powerpoint
PPTX
Rotation powerpoint
PPTX
That Conference Date and Time
PDF
Clock 2
Best Practices in Reporting Time Duration in Biometrics
Date and Time Odds Ends Oddities
Geographic Coordinate System.pdf
Introduction to Timekeeping
A brief history of Leap Seconds
Time, Why You Punish Me - Cascadia PHP 2024
GD-VII-Ch-13-Time-and-Motionnnnnnnnnnn-pdf.pptx
Lesson 05 - Time in Distrributed System.pptx
Java 8 date & time api
Class 7 13 time and motion ppt
Lab time zones -
Lab Time Zones -
class7 time motion chapter 8 science ppt.pptx
Date and Time MomentJS Edition
Unit V Synchronization of distributed system.pptx
Motion And Time
Rotation powerpoint
Rotation powerpoint
That Conference Date and Time
Clock 2
Ad

More from Justin Lin (20)

PPTX
Ch14 簡介 Spring Boot
PPTX
Ch13 整合 Spring MVC/Security
PPTX
Ch12 Spring 起步走
PPTX
Ch11 簡介 JavaMail
PPTX
Ch10 Web 容器安全管理
PPTX
Ch09 整合資料庫
PPTX
Ch08 自訂標籤
PPTX
Ch07 使用 JSTL
PPTX
Ch06 使用 JSP
PPTX
Ch05 Servlet 進階 API、過濾器與傾聽器
PPTX
Ch04 會話管理
PPTX
Ch03 請求與回應
PPTX
Ch02 撰寫與設定 Servlet
PPTX
CH1. 簡介 Web 應用程式
PDF
14. 進階主題
PDF
13.並行、平行與非同步
PDF
12. 除錯、測試與效能
PDF
11. 常用內建模組
PDF
10. 資料永續與交換
PDF
9. 資料結構
Ch14 簡介 Spring Boot
Ch13 整合 Spring MVC/Security
Ch12 Spring 起步走
Ch11 簡介 JavaMail
Ch10 Web 容器安全管理
Ch09 整合資料庫
Ch08 自訂標籤
Ch07 使用 JSTL
Ch06 使用 JSP
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch04 會話管理
Ch03 請求與回應
Ch02 撰寫與設定 Servlet
CH1. 簡介 Web 應用程式
14. 進階主題
13.並行、平行與非同步
12. 除錯、測試與效能
11. 常用內建模組
10. 資料永續與交換
9. 資料結構

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Monthly Chronicles - July 2025
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Review of recent advances in non-invasive hemoglobin estimation
Cloud computing and distributed systems.
Teaching material agriculture food technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
Modernizing your data center with Dell and AMD
Diabetes mellitus diagnosis method based random forest with bat algorithm

Joda-Time & JSR 310 – Problems, Concepts and Approaches

  • 1. Joda-Time & JSR 310 – Problems, Concepts and Approaches Justin Lin caterpillar@openhome.cc http://openhome.cc
  • 2. Agenda • Date and Calendar Problems • Time ABC • Joda-Time • JSR310 2 / 66
  • 4. What's wrong? • I am 818 years old? Rounding off error 4 / 66
  • 5. Fixed • I am 38 years old. There's a long type. All other operands are promoted. 5 / 66
  • 6. • Taiwan Java Developer Day is 1913/9/2? What's wrong? Deprecated 6 / 66
  • 7. • Taiwan Java Developer Day is 2013/9/2? What's wrong? This calendar field begins from 0. 7 / 66
  • 8. • Taiwan Java Developer Day is 2013/8/2. Fixed Use the constant variable of Calendar. 8 / 66
  • 9. • Days between two calendars is zero? What's wrong? The Calendar instance is mutable and state-reserved. 9 / 66 0
  • 10. Fixed clone() the instance , or create a new instance. 10 / 66
  • 11. The Date instance is not a date • It represents a specific instant in time, with millisecond precision. – For example, 1375430498832 milliseconds after "the epoch", namely January 1, 1970, 00:00:00 UTC. 11 / 66
  • 12. • Prior to JDK 1.1, It allowed the interpretation of dates as year, month, day, hour, minute, and second values. But…wired values… – A year y is represented by the integer y - 1900. – A month, hour, minutes and second begin from 0. – A day of month begins from 1. 12 / 66
  • 13. • All those methods about the interpretation of dates are deprecated after JDK 1.1. • The setTime method is not deprecated. The Date instance is mutable. • Converting between a specific instant in time manually is error-prone. The Calendar class provides methods for that. But … The Date instance is not a specific instant 13 / 66
  • 14. • A set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on. – A month, hour, minute and second begin from 0. – Remember to use Calendar.JANUARY. • The Calendar instance is mutable. Methods such as add, set and roll changes the state of the instance. Using Calendar can be painful and tedious 14 / 66
  • 16. Time the time • Greenwich Mean Time(GMT) – Originally referred to the mean solar time at the Royal Observatory in Greenwich, London. – Noon Greenwich Mean Time is the moment when the sun reaches its highest point in the sky. – GMT is sometimes used loosely and arguably as a synonym for UTC. • Universal Time(UT) – Observe stars as they crossed a meridian. – In 1935, the term Universal Time was recommended as a more precise term than Greenwich Mean Time. – GMT is the same as UT before 1972. 16 / 66
  • 17. • International Atomic Time(TAI) – The SI( International System of Units) second was defined in terms of the caesium atom in 1967. – Synchronised with Universal Time at the beginning of 1958. • Coordinated Universal Time(UTC) – Based on TAI. Introduced on at the beginning 1972. – With leap seconds added at irregular intervals to compensate for the slowing of Earth's rotation. – Since 30 June 2012 when the last leap second was added, TAI has been exactly 35 seconds ahead of UTC. 17 / 66
  • 18. • Unix time – A system for describing instants in time. – Defined as the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970, not counting leap seconds. • The epoch(date) – An instant in time chosen as the origin of a particular era. – The Unix epoch is the time 00:00:00 UTC on 1 January 1970. 18 / 66
  • 19. Chronologies • Julian calendar – A reform of the Roman calendar introduced by Julius Caesar in 46 BC, took effect in 45 BC, and widely used from about 4CE to 1582CE. – Defines a leap year as once every four years. • Gregorian calendar – A reform of the Julian calendar. – The last day of the Julian calendar was Thursday, 4 October 1582. – Friday, 15 October 1582 was the first day of the Gregorian calendar. 19 / 66
  • 20. • Gregorian calendar (continued) – Britain and the British Empire (including the eastern part of what is now the United States) adopted the Gregorian calendar in 1752. 20 / 66
  • 21. • Gregorian calendar (continued) – The concrete subclass GregorianCalendar of Calendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems. – setGregorianChange sets the Gregorian Calendar change date. Default is October 15, 1582 (Gregorian). 21 / 66 Fri Oct 15 1582 Thu Oct 4 1582
  • 22. • The ISO8601 standard – An international standard covering the exchange of date and time-related data. – Provide an unambiguous and well-defined method of representing dates and times. • yyyy-mm-ddTHH:MM:SS.SSS • yyyy-dddTHH:MM:SS.SSS • yyyy-Www-dTHH:MM:SS.SSS • ... 22 / 66
  • 23. • The ISO8601 standard (continued) – When representing dates and times, the Gregorian and ISO8601 differ slightly. – In ISO8601, "19" refer to the century from 1900 to 1999 inclusive. – In Gregorian, the 19th century is 1801 to 1900 inclusive. From: https://en.wikipedia.org/wiki/ISO_8601 23 / 66
  • 24. Time Zones • A region that has a uniform standard time for legal, commercial, social, and political purposes. • Most of the time zones on land are offset from UTC (UTC−12 to UTC+12) – A time change of one hour is required for each change of longitude by 15°. – The UTC time zone is sometimes denoted by Z. 24 / 66
  • 25. • Some countries, such as China and India, use a single time zone. https://upload.wikimedia.org/wikipedia/commons/a/ad/Standard_time_zones_of_the_world.png 25 / 66
  • 26. • Some higher latitude countries use daylight saving time (summer time) for part of the year, typically by changing clocks by an hour. • Taiwan DST were from 1945 to 1961 and 1974 to 1975. 26 / 66
  • 27. • JDK time zone data is updated when the JDK is updated. • Timezone Updater Tool (aka TZUpdater) – http://www.oracle.com/technetwork/java/javase/ downloads/tzupdater-download-513681.html 27 / 66
  • 29. Joda-Time • Created in 2002. • Released as v1.0 in 2005. • Released as v2.0 In 2011. • Currently v2.2. Stephen Colebourne 29 / 66
  • 30. Key concepts • Instant – Defined as an instant in the datetime continuum specified as a number of milliseconds from 1970-01-01T00:00Z. • The ReadableInstant defines an instant in the datetime continuum. – Instant – DateTime – DateMidnight – MutableDateTime 30 / 66 Immutable
  • 31. • Instant (continued) – The millisecond instant can be converted to any date time field using a Chronology. Instant Time-line 31 / 66
  • 32. • Partial – A partial date and time representation. – May 26 could apply to any year. 13:06 p.m. could apply to any day of any year. • The ReadablePartial interface defines a partial time. – LocalDate、LocalTime、LocalDateTime – YearMonth、MonthDay – Partial – YearMonthDay – TimeOfDay 32 / 66
  • 33. • Partial (continued) Instant Time-line = Partial + missing fields + time zone 33 / 66
  • 34. • Interval – An interval of time between two instants. • Interval is defined by the ReadableInterval interface. – Interval – MutableInterval 34 / 66
  • 35. • Interval (continued) Start instant End instant Interval 35 / 66
  • 36. • Duration – Represents a duration of time measured in milliseconds. • Duration is represented by the ReadableDuration interface. – Duration instant + Duration = instant 36 / 66
  • 37. • Period – Represents the same concept as Duration, but in "human" terms such as years, months, and weeks. • Period is represented by the ReadablePeriod interface. – Period – MutablePeriod – Years、Months、Weeks、Days – Hours、Minutes、Seconds 37 / 66
  • 38. • Period (continued) instant + Period = instant 38 / 66
  • 39. • Chronology – A chronology is a pluggable calendar system. • The Chronology class provides access to the individual date time fields. – ISO8601 (Default) - ISOChronology – Gregorian - GregorianChronology – GregorianJulian – GJChronology – Julian - JulianChronology – Buddhist - BuddhistChronology – Coptic - CopticChronology – Ethiopic - EthiopicChronology – Islamic - IslamicChronology GregorainCalendar replacement 39 / 66
  • 41. • Time Zone – Joda-Time also compiles the time zone data into our jar file. – You can update the raw data and recompile the jar at any time. – http://joda-time.sourceforge.net/tz_update.html • Available Time Zones – http://joda-time.sourceforge.net/timezones.html 41 / 66
  • 42. Beef • What do you need? • Period between two Instants or Partials. 42 / 66
  • 43. • What do you need? • Most of the time, we need Partial. 43 / 66
  • 44. • What do you need? • Period. 44 / 66
  • 45. • Adding 5 days, 6 months, and 3 weeks to a date and printing the formatted result. • Using Joda-Time. 45 / 66
  • 46. 46 / 66 JS R 31 0
  • 47. Joda-Time as JSR-310? • Why JSR-310 isn't Joda-Time – http://blog.joda.org/2009/11/why-jsr-310-isn- joda-time_4941.html • Joda-Time has design flaws – Human/Machine timelines – Pluggable chronology – Nulls – Internal implementation Stephen Colebourne 47 / 66
  • 48. Machine/Human timelines • Machines have one view - a single, ever increasing number. • Humans have a totally different view of time. – Calendar systems. – Years, months, days, hours, minutes and seconds. – Time zones – ... • In Joda-Time, the DateTime class (human view of an instant in time) are implementations of ReadableInstant. This is wrong. 48 / 66
  • 49. JSR 310 • The java.time package. • The distinction between computer-related times and human-related times have been made more apparent. 49 / 66
  • 50. Computer-related times • Instant – Represents a fixed point in time as an offset from the standard Java epoch (1st Jan 1970). The instant is stored to nanosecond resolution. – Defines its own time-scale, the Java Time-Scale. – http://download.java.net/jdk8/docs/api/java/time /Instant.html 50 / 66
  • 51. Human-related times • Without a time-zone in the ISO-8601 – LocalDateTime – LocalDate – LocalTime • Years, months, days – Year, such as 2007. – YearMonth, such as 2007-12. – MonthDay, such as 12-03. 51 / 66
  • 52. An amount of time • A time-based amount of time. – Duration, such as '34.5 seconds'. –Uses nanosecond resolution with a maximum value of the seconds that can be held in a long. • A date-based amount of time – Period, such as '2 years, 3 months and 4 days'. 52 / 66
  • 53. Joda-Time vs JSR310 • Joda-Time • JSR310 53 / 66
  • 55. • Joda-Time • Using JSR310. 55 / 66
  • 56. • OffsetDateTime adds to the instant the offset from UTC in ISO-8601. • ZonedDateTime adds full time-zone rules 56 / 66
  • 57. Framework-level API • The java.time.temporal package. • TemporalAccessor – Defines read-only access to a temporal object, such as a date, time, offset or some combination of these. • Temporal extends TemporalAccessor. – The base interface type for date, time and offset objects that are complete enough to be manipulated using plus, minus and with. 57 / 66
  • 58. • Temporal implementations. – Instant – LocalDate、LocalDateTime、LocalTime – OffsetDateTime、OffsetTime – Year、YearMonth – ZonedDateTime • MonthDay implements TemporalAccessor rather than Temporal. Why? – http://download.java.net/jdk8/docs/api/java/time/M onthDay.html 58 / 66
  • 59. • plus and minus of Temporal – plus(TemporalAmount amount) – plus(long amountToAdd, TemporalUnit unit) – minus(TemporalAmount amount) – minus(long amountToSubtract, TemporalUnit unit) • TemporalAmount defines an amount of time, such as "6 hours", "8 days" or "2 years and 3 months". – Duration – Period 59 / 66
  • 60. • TemporalUnit defines a unit of date-time, such as Days or Hours. 60 / 66
  • 61. • with of Temporal – with(TemporalField field, long newValue) – with(TemporalAdjuster adjuster) • TemporalField defines a field of date-time, such as month-of-year or hour-of-minute. • Use TemporalAdjuster for more complex alterations 61 / 66
  • 62. Chronology? • What is the range of values returned by this method in Joda-Time? • The answer is not 1 to 12, but could be 1 to 13! – The Coptic chronology has 13 months in a year, and thus can return a range of 1 to 13. • Most users of the API never check to see if the chronology is the standard ISO chronology. 62 / 66
  • 63. • A better solution would be to keep the date/time classes restricted to a single calendar system. • Those date and time classes located in the java.time package are in the ISO-8601 calendar system. • Check the java.time.chrono package if you need other chronologies. – JapaneseChronology – ThaiBuddhistChronology – … 63 / 66
  • 64. Summery • Using Date, Calendar and existing date- related APIs can be error-prone, painful and tedious. • The complexities of accurate timekeeping are beyond your imagine. • What do you need? Computer-related times or human-related times? • Need to manipulate dates and times? Using Joda-Time or JSR310 to make it easy! 64 / 66
  • 65. • Other libraries for handling dates and times – Date4j, Simple Alternative To java.util.Date. – Arrow, Better dates and times for Python. – Moment.js, A JavaScript date library for parsing, validating, manipulating, and formatting dates. – Noda-Time, A port of Joda-Time to .NET 65 / 66
  • 66. References • Joda-Time – http://joda-time.sourceforge.net/ • Joda-Time - You can't escape time. Why not make it easy? – http://www.ibm.com/developerworks/java/library/j- jodatime/index.html • JSR 310 Date and Time API for Java – http://www.infoq.com/news/2010/03/jsr-310 • Why JSR-310 isn't Joda-Time – http://blog.joda.org/2009/11/why-jsr-310-isn-joda- time_4941.html • JDK8 Javadoc – http://download.java.net/jdk8/docs/api/index.html 66 / 66