Professional Documents
Culture Documents
Joda Time
Joda Time
Java Library
Mark Volkmann
Object Computing, Inc.
mark@ociweb.com
2 Joda Time
1
Supported Concepts
• Instant
partial +
“an instant in the datetime continuum specified as
missing fields +
a number of milliseconds from 1970-01-01T00:00Z”
time zone =
• Partial instant
• Interval
“an interval of time from one millisecond instant to another”
• Duration
“a duration of time measured in milliseconds”
• Period
“a period of time defined in terms of fields,
for example, 3 years 5 months 2 days and 7 hours”
• Chronology
“a pluggable calendar system”
3 Joda Time
Mutability
4 Joda Time
2
org.joda.time Main Classes
Duration Duration
5 Joda Time
Instants …
Output:
milliseconds = 1470
now date and time = 2008-12-22T10:07:26.468-06:00
6 Joda Time
3
… Instants
// An instant representing a specific date and time
// can be created by specifying all the field values.
dt = new DateTime(1961, 4, 16, 10, 19, 0, 0);
System.out.println("my birthday = " + dt);
System.out.println("year is " + dt.getYear());
System.out.println("month is " + dt.getMonthOfYear())
Output:
my birthday = 1961-04-16T10:19:00.000-06:00
year is 1961
month is 4
7 Joda Time
Partials …
Output:
millis = -274866753528 (negative because it’s before 1970)
millis = 58046472 (close to zero)
8 Joda Time
4
… Partials
LocalDate today = new LocalDate();
int year = today.getYear();
Output:
The next Christmas is on 2008-12-25
9 Joda Time
Intervals …
Output:
interval = 2008-12-22T10:07:26.466/2009-01-22T10:07:26.466
start = 2008-12-22T10:07:26.466-06:00
end = 2009-01-22T10:07:26.466-06:00
duration = PT2678400S (31*24*60*60)
10 Joda Time
5
… Intervals
// There are many Interval methods whose name begins with "with"
// that create a new interval relative to an existing one.
// For example, this creates an interval
// that lasts for one more hour.
interval = interval.withEnd(interval.getEnd().plusHours(1));
System.out.println("interval = " + interval);
Output:
interval = 2008-12-22T10:07:26.466/2009-01-22T11:07:26.466
11 Joda Time
Durations …
12 Joda Time
6
… Durations …
// A Duration can be obtained from an Interval.
Interval interval = new Interval(now.toDateMidnight(), now);
duration = interval.toDuration();
Output:
System.out.println("duration = " + duration); duration = PT36446.333S
Output:
nowPlusOneSecond = 2008-12-22T10:07:27.333-06:00
13 Joda Time
… Durations
// There is no mutable Duration type,
// so a new Duration object must be created
// when time needs to be added or subtracted.
// Compare this to code in the partials examples.
duration = duration.plus(100);
duration = duration.minus(20);
System.out.println("duration = " + duration)
Output:
duration = PT1.080S
14 Joda Time
7
Periods …
… Periods …
// Periods must be converted to durations in order to
// compare them. This is because the exact length of period
// can vary based on context. For example, a period of one day
// can be 23 hours instead of 24 when the context is
// a day in which daylight savings time is changing.
Output:
hours = PT2H
minutes = PT177M
16 Joda Time
8
… Periods
// Using a MutablePeriod is the most efficient way
// to represent a duration that needs to be modified
// when concurrent access isn't an issue.
// Compare this to code in the durations examples.
MutablePeriod mp = new MutablePeriod(1000);
mp.add(100);
mp.add(-20);
System.out.println("mp = " + mp);
Output:
mp = PT1.080S
17 Joda Time
Chronologies
18 Joda Time
9
JSR-310
19 Joda Time
10