Joda Time Java Library. Joda Time Overview

Similar documents
DATE AND TIME FOR NINE MILLION JAVA DEVELOPERS

Introduction to Java 8 Date Time API

JSR 310 Date and Time API Jan 24th, Stephen Colebourne, OpenGamma Ltd. Roger Riggs, Oracle Inc.

To Java SE 8, and Beyond (Plan B)

ETHERNET IRRIGATION CONTROLLER. Irrigation Caddy Model: ICEthS1. User Manual and Installation Instructions

Documentation to use the Elia Infeed web services

Astromechanics. 1 solar day = sidereal days

Java SE 8 Programming

WORKING WITH SAS DATE AND TIME FUNCTIONS Andrew H. Karp Sierra Information Services, Inc. San Francisco, California USA

ASTRONOMY 161. Introduction to Solar System Astronomy

Time Synchronization & Timekeeping

50 Things You May Not Know You Can Do With The 4GL

LICENSE4J AUTO LICENSE GENERATION AND ACTIVATION SERVER USER GUIDE

Statistical Confidence Calculations

Mass Announcement Service Operation

JAVA - DATE & TIME. Java provides the Date class available in java.util package, this class encapsulates the current date and time.

.NET Standard DateTime Format Strings

VB.NET - DATE & TIME

Mineral rights ownership what is it and why is it so unique in the USA?

Limitations of Data Encapsulation and Abstract Data Types

Activity 1 Reading Universal Time Level 2

Quantitative vs. Categorical Data: A Difference Worth Knowing Stephen Few April 2005

Time Zones and MySQL. Presented by: Sheeri K. Cabral

Google Calendar 3 Version 0.8 and 0.9 Installation and User Guide. Preliminary Setup

Core Fittings C-Core and CD-Core Fittings

Welcome to the financial reports topic

ShoreTel 13 Conferencing User Guide. Part Number

Merchant Interface Online Help Files

ImageNow Report Library Catalog

WebEx Meeting Center User Guide

Drupal 6 Site Builder Solutions

REPRINT. Release User s Guide. iseries (AS/400) Developed and Distributed by

Performance Comparison of low-latency Anonymisation Services from a User Perspective

Core Components Data Type Catalogue Version October 2011

JIDE Gantt Chart Developer Guide

Quick and Easy Solutions With Free Java Libraries Part II

Section 1.4. Java s Magic: Bytecode, Java Virtual Machine, JIT,

Garbage Collection in NonStop Server for Java

Basic numerical skills: FRACTIONS, DECIMALS, PROPORTIONS, RATIOS AND PERCENTAGES

Weaving Stored Procedures into Java at Zalando

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

OPENRULES. Getting Started. Open Source Business Decision Management System. Release OpenRules, Inc.

SnapLogic Salesforce Snap Reference

PREPARATION FOR MATH TESTING at CityLab Academy

Charlesworth School Year Group Maths Targets

In order to describe motion you need to describe the following properties.

Java SE 8 - Java Technologie Update

Used as content for outbound telesales programmes and (potentially) inbound telesales response.

ORCA-Registry v1.0 Documentation

Unit Two Practice Test: Powers and Exponent Laws

CORISECIO. Quick Installation Guide Open XML Gateway

=

Cloud Services. Archiving. End User Guide

CALCULATION DIFFERENCES WHEN IMPORTING FROM INTO ORACLE PRIMAVERA P6 VERSION 7 PAUL E HARRIS EASTWOOD HARRIS

BlackBerry Enterprise Server Wireless Software Upgrades Version: 4.1 Service Pack: 7. Administration Guide

Multiplying and Dividing Listen & Learn PRESENTED BY MATHMANIAC Mathematics, Grade 8

PAYLINE USER GUIDE. 1 Logging into Payline. 2 - Processing a Purchase

Spark ΕΡΓΑΣΤΗΡΙΟ 10. Prepared by George Nikolaides 4/19/2015 1

Symantec Enterprise Vault

Calculating average acceleration from velocity change and time

Package TimeWarp. R topics documented: April 1, 2015

April Online Payday Loan Payments

Manual. Version: 1.0.0

Levels 1, 2 & 3 Family Edition. How to Download the Core Software from the Website

Paul Brebner, Senior Researcher, NICTA,

Contents. 9-1 Copyright (c) N. Afshartous

What Is Singapore Math?

GFI WebMonitor Administration and Configuration Manual

Table of Contents. Introduction How to access the Safari Backoffice How Safari corporate accounts are structured...

International Payments

Client Marketing: Sets

Exponent Law Review b. 1 d. 0. x 5 d. x 11. a 5 b. b 8 a 8. b 2 a 2 d. 81u 8 v u 8 v Name: Class: Date:

ideal Merchant Integration Guide

Follow links Class Use and other Permissions. For more information, send to:

Kaplan Higher Education Seminar Student User Guide

Introduction. Multicurrency Improving speed and performance. Blackbaud Enterprise CRM Multicurrency

ESS Leave & Time Employee Leave Request

Chapter Two. THE TIME VALUE OF MONEY Conventions & Definitions

Quick Start. Guide. The. Guide

Performing Simple Calculations Using the Status Bar

Java Servlet 3.0. Rajiv Mordani Spec Lead

USB Recorder User Guide

INSTALLING PowerScribe Workstation Client Software On A System That Has Never Had PowerScribe Installed On It

GUIDE TO GOOGLE ADWORDS

Release Bulletin EAServer for HP-UX Itanium and IBM AIX

AACE International Recommended Practices

INSPIRE Dashboard. Technical scenario

workforceiq Job Despatch and Workforce Management software for Smartphones

3. Time value of money. We will review some tools for discounting cash flows.

The Java EE 6 Platform. Alexis Moussine-Pouchkine GlassFish Team

LICENSE4J FLOATING LICENSE SERVER USER GUIDE

GroupWise Calendar GroupWise Task Lists GroupWise Reminder Notes Recurring Events Proxy Access Shared Folders

CHAPTER 5. Obfuscation is a process of converting original data into unintelligible data. It

Transcription:

Joda Time Java Library Mark Volkmann Object Computing, Inc. mark@ociweb.com Joda Time Overview Free, open-source Java library for working with dates and times Apache V2 license; not viral Replacement for the JDK Date and Calendar classes being standardized under JSR-310 likely to become a standard part of Java Small - version 1.6 JAR file is 524 KB No dependencies outside core Java classes http://joda-time.sourceforge.net/ API documentation is at a link on this page http://joda-time.sourceforge.net/api-release/ 2 Joda Time 1

Supported Concepts Instant Partial Interval an instant in the datetime continuum specified as a number of milliseconds from 1970-01-01T00:00Z a partial date/time representation (subset of fields) with no time zone an interval of time from one millisecond instant to another Duration Period a duration of time measured in milliseconds 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 partial + missing fields + time zone = instant 3 Joda Time Mutability Most classes that represent the concepts are immutable Using these is recommended for most cases to avoid issues with concurrent access The only mutable classes are MutableDateTime MutableInterval MutablePeriod 4 Joda Time 2

org.joda.time Main Classes Concept Instant Partial Interval Duration Period Chronology Immutable Classes DateTime DateMidnight Instant LocalDate LocalDateTime LocalTime Partial Interval Duration Period Minutes Hours Days Weeks Months Years GregorianChronology ISOChronology (default) many more Mutable Classes MutableDateTime MutableInterval MutablePeriod 5 Joda Time Instants A number of milliseconds from 1970-01-01T00:00Z Instant instant = new Instant(1000); // 1 second after 1970 instant = instant.plus(500); instant = instant.minus(30); System.out.println("milliseconds = " + instant.getmillis()); // Creating an instant representing current date and time is easy. DateTime dt = new DateTime(); System.out.println("now date and time = " + dt); 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()) my birthday = 1961-04-16T10:19:00.000-06:00 year is 1961 month is 4 7 Joda Time Partials A partial date/time representation (subset of fields) with no time zone // There are a large number of constructors for creating // period objects. We'll demonstrate a few of them. LocalDate birthday = new LocalDate(1961, 4, 16); long millis = birthday.todatetimeatcurrenttime().getmillis(); System.out.println("millis = " + millis); birthday = new LocalDate(1970, 1, 1); // not 0ms in local timezone millis = birthday.todatetimeatcurrenttime().getmillis(); System.out.println("millis = " + millis); 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(); // Find the date of the next Christmas. LocalDate christmas = new LocalDate(year, 12, 25); if (today.isafter(christmas)) christmas = christmas.plusyears(1); System.out.println("The next Christmas is on " + christmas); The next Christmas is on 2008-12-25 9 Joda Time Intervals An interval of time from one millisecond instant to another // Create an Interval from now to one month from now. DateTime starttime = new DateTime(); // now DateTime endtime = starttime.plus(months.months(1)); Interval interval = new Interval(startTime, endtime); System.out.println("interval = " + interval); System.out.println("start = " + interval.getstart()); System.out.println("end = " + interval.getend()); System.out.println("duration = " + interval.toduration()); 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); interval = 2008-12-22T10:07:26.466/2009-01-22T11:07:26.466 11 Joda Time Durations A duration of time measured in milliseconds // Can construct with a given number of milliseconds. Duration duration = new Duration(1000); // 1 second System.out.println("duration = " + duration); duration = PT1S // Can construct with two Instants // to get the duration between them. // Get the duration from midnight this morning to now. DateTime now = new DateTime(); System.out.println("now = " + now); // The method todatemidnight returns a new DateTime // where all the time fields are set to zero. duration = new Duration(now.toDateMidnight(), now); System.out.println("duration = " + duration); now = 2008-12-22T10:07:26.333-06:00 duration = PT36446.333S 12 Joda Time 6

Durations // A Duration can be obtained from an Interval. Interval interval = new Interval(now.toDateMidnight(), now); duration = interval.toduration(); System.out.println("duration = " + duration); duration = PT36446.333S // A Duration can be added to an Instant to get a new Instant. duration = new Duration(1000); // 1 second DateTime nowplusonesecond = now.plus(duration); System.out.println("nowPlusOneSecond = " + nowplusonesecond); 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) duration = PT1.080S 14 Joda Time 7

Periods A period of time defined in terms of fields Period period = new Period(1000); // 1 second System.out.println("period = " + period); period = PT1S // A Period can be constructed with field values // or a duration in milliseconds. Create a period of // 2 hours 57 minutes 15 seconds 0 milliseconds. period = new Period(2, 57, 15, 0); System.out.println("period = " + period); period = PT2H57M15S // A Period can be added to an instant to get a new instant. // The date specified in the next line is March 4, 2007, 8 am. // Unlike in the JDK, the value for January is 1 instead of 0. DateTime starttime = new DateTime(2007, 3, 4, 8, 0, 0, 0); DateTime endtime = starttime.plus(period); System.out.println("endTime = " + endtime); endtime = 2007-03-04T10:57:15.000-06:00 15 Joda Time 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. Hours hours = Hours.hoursBetween(startTime, endtime); System.out.println("hours = " + hours); Minutes minutes = Minutes.minutesBetween(startTime, endtime); System.out.println("minutes = " + minutes); 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); mp = PT1.080S 17 Joda Time Chronologies Calendar system options Gregorian - standard since 10/15/1582 defines every fourth year as leap, unless the year is divisible by 100 and not by 400 Julian - standard before 10/15/1582 defines every fourth year as leap GJ historically accurate with Julian followed by Gregorian starting on 10/15/1582 ISO - the default based on the ISO8601 standard same as Gregorian except for treatment of century Buddhist, Coptic, Ethiopic - not commonly used For most applications, the default ISOCronology is appropriate 18 Joda Time 9

JSR-310 From Stephen Colebourne, creator of Joda Time and JSR-310 spec. lead JSR-310 is inspired by Joda-Time, rather than a straight adoption of it. Thus, there are key differences in the APIs. JSR-310 isn't at the download and use stage yet. To keep up with the status of this standardization effort, browse https://jsr-310.dev.java.net/ 19 Joda Time 10