DateUtil { public static String formatISO8601(final Date date) { if (date == null) return ""; SimpleDateFormat dateFormat = new SimpleDateFormat(ISO8601_DATE_FORMAT_PATTERN); return dateFormat.format(date); }  static Date parseISO8601(final String date); static String formatISO8601(final Date date);  }
@Test public void formatISO8601() throws Exception { final Calendar calStockholm = Calendar.getInstance(TimeZone.getTimeZone("Europe/Stockholm")); calStockholm.set(2010, 0, 1, 12, 34, 0); calStockholm.set(Calendar.MILLISECOND, 0); Assert.assertEquals("2010-01-01", DateUtil.formatISO8601(calStockholm.getTime())); final Calendar calStockholm2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Stockholm")); calStockholm2.set(2010, 0, 1, 12, 34, 56); calStockholm2.set(Calendar.MILLISECOND, 0); Assert.assertEquals("2010-01-01", DateUtil.formatISO8601(calStockholm2.getTime())); final Calendar calUtc = Calendar.getInstance(TimeZone.getTimeZone("UTC")); calUtc.set(2010, 0, 1, 12, 34, 56); calUtc.set(Calendar.MILLISECOND, 0); Assert.assertEquals("2010-01-01", DateUtil.formatISO8601(calUtc.getTime())); }