Code example for SimpleDateFormat

Methods: formatsetTimeZone

        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);
        assertEquals(date, sdf.format(new Date(0)));
        sdf = new SimpleDateFormat(fmt, Locale.US);
        assertEquals(date, sdf.format(new Date(0)));
    public void testTimeZoneFormatting() throws Exception { 
        Date epoch = new Date(0);
        // Create a SimpleDateFormat that defaults to 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: 
Connect your IDE to all the code out there  Get Codota for Java