Jan 7, 2014

Working with Date and Time in Java

Q. What is the difference between java.sql.Date and java.util.Date?
A. java.util.Date supports both date and time. java.sql.Date supports only date, java.sql.Time supports only time, and java.sql.TimeStamp supports both date and time.

Q. Why should you favor using the joda-time library for date time operations?
A. The Java Date and Calendar classes are badly designed. The Joda Time library is less verbose and more intuitive. Here are a few examples to prove it.

Example 1: You may think it is midnight 1st of Jan 2009

Calendar cal = Calendar.getInstance();
cal.set(2009, 1, 1, 0, 0, 0);

But the above one represents 1st of Jan 2009 because months are zero based. There is no consistency. Some indexes start with zero and others start with 1. Better approach is to use

Calendar cal = Calendar.getInstance();
cal.set(2009, Calendar.JANUARY, 1, 0, 0, 0);

Joda is even better

DateTime dateTime = new DateTime(2009, 1, 1, 0, 0, 0, 0);

Example 2: To add 60 days to the above date, in Java Date

cal.add(Calendar.DAY_OF_MONTH, 60);

In Joda, it is more intuitive


Example 3: To format the above in Java Date

final String FORMAT = "yyyy/MMM/dd HH:mm:ss";
// define it locally as this class is not thread-safe
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
System.out.println(sdf.format(cal.getTime( )));  

In Joda

final String FORMAT = "yyyy/MMM/dd HH:mm:ss";

No wonder why this library will be included in Jav 8. Till include the Joda jar to your project.

Q. In Joda, can you explain the concepts of Instant, Duration, Partial, and Period?

Instant: is the most commonly used concept in Joda. It is a point in time in nanoseconds from January 1st 1970. It is an immutable class. If you want to mutate, then use MutableDateTime class.

DateTime dt = new DateTime();  // current time
int month = dt.getMonth();     // gets the current month
int month = dt.month().get();  // alternative way to get value

Duration: is the amount of time measured in nanoseconds.

instant + duration = instant

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);

// duration in ms between two instants
Duration dur = new Duration(start, end);

// calc will be the same as end
DateTime calc =;

Partial: is a partial date and time representation. All implementations represent local dates and times, and do not reference a time zone. E.g. LOcalDate, LocalDateTime, LocalTime, etc.

partial + missing fields + time zone = instant

LocalDate date = new LocalDate(2004, 12, 25);
LocalTime time = new LocalTime(12, 20);

// merge, resulting in 2004-25-12T12:20 (default time zone)
DateTime dt = date.toDateTime(time);

Period: is a period of time defined in terms of fields, for example, 2 years 3 months 5 days and 7 hours. This differs from a duration in that it is inexact in terms of milliseconds.

LocalTime time =;
LocalTime newTime;
Period p = Period.of(5, HOURS);
//add 5 hours to current time
newTime =;

Q. How will you round the time to a minute?

 Instant date = new Instant(); // immutable
 MutableDateTime dt = new MutableDateTime(date); //mutable
 dt.minuteOfDay().roundFloor(); // rounds time to a minute



Post a Comment

Subscribe to Post Comments [Atom]

<< Home