### Question:
DateFormatNormalizer implements Normalizer { @Override public String normalize(String value) throws NormalizationException { try { DateFormat in = getLocaleDateFormat(incomingFormat, incomingLocale); if (this.timezone != null) in.setTimeZone(this.timezone); in.setLenient(false); Date date = in.parse(value); DateFormat out = getLocaleDateFormat(outgoingFormat, outgoingLocale); out.setTimeZone(TimeZone.getTimeZone("GMT")); return out.format(date); } catch (Throwable t) { throw new NormalizationException(t); } } DateFormatNormalizer(String incomingFormat, String outgoingFormat); void setTimeZone(String timeZoneCode); @Override String normalize(String value); void setIncomingLocale(Locale incomingLocale); void setOutgoingLocale(Locale outgoingLocale); String getIncomingFormat(); String getOutgoingFormat(); TimeZone getTimezone(); Locale getIncomingLocale(); Locale getOutgoingLocale(); static final String ISO_8601_FORMAT; }### Answer:
@Test public void normalizeValidSystemTimezone() throws NormalizationException { int offset = new GregorianCalendar().get(Calendar.ZONE_OFFSET); int offsetHrs = offset / 1000 / 60 / 60; int gmtHours = 20; int localHours = gmtHours + offsetHrs; String gmtBeginningDateTime = "2013-02-24T"; String gmtEndDateTime = ":13:44.000Z"; String localEndDateTime = ":13:44 on 02/24/13"; assertEquals(gmtBeginningDateTime + gmtHours + gmtEndDateTime, this.dateNormalizer.normalize(localHours + localEndDateTime)); }
@Test public void normalizeIgnoringTimezone() throws NormalizationException { DateFormatNormalizer normalizer = new DateFormatNormalizer("HH:mm:ss Z' on 'MM/dd/yy", DateFormatNormalizer.ISO_8601_FORMAT); assertEquals("2013-02-24T20:13:44.000Z", normalizer.normalize("15:13:44 -0500 on 02/24/13")); assertEquals("2013-02-24T20:13:44.000Z", normalizer.normalize("16:13:44 -0400 on 02/24/13")); assertEquals("2013-02-24T20:13:44.000Z", normalizer.normalize("12:13:44 -0800 on 02/24/13")); }
@Test(expected = NormalizationException.class) public void normalizeInvalidIncomingFormat() throws NormalizationException { this.dateNormalizer.normalize("12:13:44 on 24/2/13"); }
@Test(expected = NormalizationException.class) public void provideInvalidFormatOnConstruction() throws NormalizationException { Normalizer testNormalizer = new DateFormatNormalizer("kk:mm:ssq bad data shouldn't work here", DateFormatNormalizer.ISO_8601_FORMAT); testNormalizer.normalize("This isn't even close"); }
@Test public void testTwitterFormat() throws NormalizationException { Normalizer normalizer = new DateFormatNormalizer("EEE MMM dd kk:mm:ss zzz yyyy", DateFormatNormalizer.ISO_8601_FORMAT); assertEquals("2012-09-27T03:28:05.000Z", normalizer.normalize("Wed Sep 26 23:28:05 EDT 2012")); }