I have a routine which receives a date in the format yyyy, MMM dd, HH:mm
in GMT time. I have to convert this String to a Date object and I do it with a SimpleDateFormat
, but now I have to take that Date object and format it in GMT-5 using again a SimpleDateFormat
, but the method is returning the same original String Date. Why? This is my routine:
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");
public static Date parseDate(String date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
Date d = null;
try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
formatter.setTimeZone(destinationTimeZone);
return formatter.format(date);
}
@Test
public void testDateConversion() {
String strDate = "2015, Aug 03, 23:50";
Date date = DateFormatter.parseDate(strDate, "yyyy, MMM dd, HH:mm");
String dateFormatted = DateFormatter.formatDate(date, "yyyy, MMM dd, HH:mm");
assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
}
Error message:
org.junit.ComparisonFailure:
Expected :2015, Aug 03, 19:50
Actual :2015, Aug 03, 23:50