Code example for SimpleDateFormat

Methods: format, setTimeZone

0
        String fmt = "yyyy-MM-dd HH:mm:ss.SSS z";
        String date = "1970-01-01 01:00:00.000 CET";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        assertEquals(date, sdf.format(new Date(0)));
        sdf = new SimpleDateFormat(fmt, Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Zurich"));
        assertEquals(date, sdf.format(new Date(0)));
    } 
 
    // http://code.google.com/p/android/issues/detail?id=8258 
    public void testTimeZoneFormatting() throws Exception { 
        Date epoch = new Date(0);
 
        // Create a SimpleDateFormat that defaults to America/Chicago... 
        TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
        // We should see something appropriate to America/Chicago... 
        assertEquals("1969-12-31 18:00:00 -0600", sdf.format(epoch));
        // We can set any TimeZone we want: 
        sdf.setTimeZone(AMERICA_LOS_ANGELES);