Archive

Posts Tagged ‘time’

Minimum to know about dates and time in Java

June 16, 2010 1 comment

Please avoid coding the following method. It has no effect and demonstrates a lack of understanding of Date and Calendar.

// BULLSHIT CODE! This method does nothing.
public static Date convertTz(Date date, TimeZone tz) {
  Calendar cal = Calendar.getInstance();
  cal.setTimeZone(TimeZone.getTimeZone("UTC"));
  cal.setTime(date);
  cal.setTimeZone(tz);
  return cal.getTime();
}

Odi has a great explanation of the bare minimums you need to understand about dates and times to make useful, real world applications.  The examples concern Java, but similar concerns must be taken in other languages as well.  Highly recommended reading.

Advertisements
Categories: Java Tags: , ,