Social Media

Joda-Time

Most project have utils packages containing utils for handling common procedures like Dates. One project worth looking at is the joda project(http://joda.sourceforge.net/) is split into 4 subprojects –

  • Joda-Time
  • Joda-Money
  • Joda-Primitives
  • Joda-Beans

The project Ive found most useful is joda-time, which provides features not available in Java’s Date and Calendar classes. There are a number of advantages to using this package, and you can read them on the joda-time website (http://www.joda.org/joda-time/). But these include multiple Calendar support, immutable objects to encourage thread safety, calendar/date mathematics and formatting.

Getting Started

Adding joda-time to your workspace is simply a matter of getting the jar and adding it to your classpath, or using maven –

[sourcecode language=”xml”] <dependency>
   <groupId>joda-time</groupId>
   <artifactId>joda-time</artifactId>
   <version>2.3</version>
<dependency>
[/sourcecode]

Examples

[sourcecode language=”java”] import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Instant;
import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.MutableDateTime;

public class JodaTimeExamples {

 public static void main(String[] args) {
  
  
  System.out.println("DateTime creation\n");
         DateTime createDateTime = null;
        
         // From constructor – Immutable
         createDateTime = new DateTime();
         System.out.println("createDateTime = " + createDateTime);
 
         // From date object – Immutable
         Date date = new Date();
         createDateTime = new DateTime(date);
         System.out.println("createDateTime = " + createDateTime);
 
         // From Calendar – Immutable
         Calendar calendar = GregorianCalendar.getInstance();
         createDateTime = new DateTime(calendar);
         System.out.println("createDateTime = " + createDateTime);
   
// LocalDate create an immutable DateTime class without TimeZone
LocalDate localDate = new LocalDate(2010, 10, 16);
System.out.println("localDate = " + localDate);
        
         //  Modifiable instance of DateTime – Mutable
         MutableDateTime mutableDateTime = new MutableDateTime();
         System.out.println("mutableDateTime = " + mutableDateTime);
       
         System.out.println("\n***************************************************\n");

System.out.println("An Instant is the number of ms since the epoch – 1970-01-01T00:00Z");
       
         // epoch – 1970-01-01T00:00Z
         Instant instant = new Instant();
 
         // Instant + 1s (1000ms)
         instant = instant.plus(1000);
 
         // Instant – 1s (1000ms)
         instant = instant.minus(1000);
         System.out.println("Milliseconds = " + instant.getMillis());
 
         System.out.println("\n***************************************************\n");
        
System.out.println("Date mathematics\n");
DateTime mathematicsDateTime = new DateTime();
   
// Adding and subtracting hours, minutes, …
System.out.println("Date mathematics – Adding and subtracting");
         System.out.println("Add 10 minutes is " + mathematicsDateTime.plusMinutes(10));
         System.out.println("Add 1 hour is " + mathematicsDateTime.plusHours(1));
 
         System.out.println("Remove 10 minutes is " + mathematicsDateTime.minusMinutes(10));
         System.out.println("Remove 1 hour is " + mathematicsDateTime.minusHours(1));  
   
System.out.println("Date mathematics – days and months between dates");
         DateTime start = new DateTime("2010-10-16");
DateTime end = new DateTime("2014-02-24");
 
// Get days between the start date and end date.
int days = Days.daysBetween(start, end).getDays();
System.out.println("Days between 2010-10-16 and 2014-02-24 – " + days);
 
// Get months between these dates.
int months = Months.monthsBetween(start, end).getMonths();
System.out.println("Months between 2010-10-16 and 2014-02-24 – " + months);
  
System.out.println("\n***************************************************\n");
    
System.out.println("Formatted DateTime\n");
DateTime formattedDateTime = new DateTime();
   
// By locale
System.out.println(formattedDateTime.toString("MM/dd/yyyy HH:mm:ss"));
System.out.println(formattedDateTime.toString("MM/dd/yyyy HH:mm:ss", Locale.GERMANY));
System.out.println(formattedDateTime.toString("MM/dd/yyyy HH:mm:ss", Locale.FRENCH));
}
}
[/sourcecode]

Output –

[sourcecode] DateTime creation

createDateTime = 2014-02-24T13:02:39.053Z
createDateTime = 2014-02-24T13:02:39.115Z
createDateTime = 2014-02-24T13:02:39.131Z
localDate = 2010-10-16
mutableDateTime = 2014-02-24T13:02:39.146Z

***************************************************

An Instant is the number of ms since the epoch – 1970-01-01T00:00Z
Milliseconds = 1393246959146

***************************************************

Date mathematics

Date mathematics – Adding and subtracting
Add 10 minutes is 2014-02-24T13:12:39.146Z
Add 1 hour is 2014-02-24T14:02:39.146Z
Remove 10 minutes is 2014-02-24T12:52:39.146Z
Remove 1 hour is 2014-02-24T12:02:39.146Z
Date mathematics – days and months between dates
Days between 2010-10-16 and 2014-02-24 – 1227
Months between 2010-10-16 and 2014-02-24 – 40

***************************************************

Formatted DateTime

02/24/2014 13:02:39
02/24/2014 13:02:39
02/24/2014 13:02:39
[/sourcecode]