I am writing a test to check the correct parsing of the date / time RSS. But, the test does not pass successfully, as I think the problem is in time zones.
Tell me what is the error?
RSSDateHelperTest.java
package com.ragmon.rssreader.helpers; import org.hamcrest.CoreMatchers; import org.junit.Test; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; import static org.junit.Assert.*; public class RSSDateHelperTest { @Test public void getFromRSSDateString_withLocal() throws ParseException { String rssDate = "Tue, 19 Oct 2004 13:38:55 -0200"; Locale locale = Locale.ENGLISH; Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("-0200")); calendar.set(2004, 10, 19, 13, 38, 55); Date date = calendar.getTime(); assertThat(RSSDateHelper.getFromRSSDateString(rssDate, locale), CoreMatchers.is(date)); } } RSSDateHelper.java
package com.ragmon.rssreader.helpers; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class RSSDateHelper { public static Date getFromRSSDateString(String dateString, Locale locale) throws ParseException { DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", locale); return formatter.parse(dateString); } public static Date getFromRSSDateString(String dateString) throws ParseException { return getFromRSSDateString(dateString, Locale.ENGLISH); } }package com.ragmon.rssreader.helpers; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class RSSDateHelper { public static Date getFromRSSDateString(String dateString, Locale locale) throws ParseException { DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", locale); return formatter.parse(dateString); } public static Date getFromRSSDateString(String dateString) throws ParseException { return getFromRSSDateString(dateString, Locale.ENGLISH); } }
The test does not pass, that's what gives out:
java.lang.AssertionError: Expected: is <Fri Nov 19 15:38:55 EET 2004> but: was <Tue Oct 19 18:38:55 EEST 2004> Expected :is <Fri Nov 19 15:38:55 EET 2004> Actual :<Tue Oct 19 18:38:55 EEST 2004>
java.lang.AssertionError: Expected: is <Tue Oct 19 16:38:55 EEST 2004> but: was <Tue Oct 19 18:38:55 EEST 2004> Expected :is <Tue Oct 19 16:38:55 EEST 2004> Actual :<Tue Oct 19 18:38:55 EEST 2004>- ragmon