Dating tips

If you're going to use both java.text.DateFormats and Joda Time DateTimeFormatters, be confident that the time zone data of both the JDK and Joda agree (they are different physical sources). Otherwise, you could end up up with head-scratchers such as this:

import static java.util.TimeZone.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateFormatting {
    public static void main(String... args) throws Exception {
        DateFormat from = new SimpleDateFormat("yyyyMMdd");
        Date parsed = from.parse("20081001");
        DateTimeFormatter to = DateTimeFormat.forPattern("MM/yyyy");
        System.out.println(to.print(parsed.getTime())); // "09/2008"

Or maybe don't intermix the two.

Consider yourself warned.

Written on November 24, 2009