0

I have a routine which receives a date in the format yyyy, MMM dd, HH:mmin 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
Joe Almore
  • 4,036
  • 9
  • 52
  • 77
  • `Calendar` may do the trick for you. See: http://stackoverflow.com/questions/6567923/timezone-conversion – Alex Aug 03 '15 at 20:50

2 Answers2

1

Solved by indicating the Timezone of the receiving date string:

public static TimeZone originTimeZone = TimeZone.getTimeZone("GMT"); // +Added
public static TimeZone destinationTimeZone = TimeZone.getTimeZone("GMT-4");

public static Date parseDate(String date, String format) {
    SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
    formatter.setTimeZone(originTimeZone);// +Added
    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);
}
Joe Almore
  • 4,036
  • 9
  • 52
  • 77
0

Try changing the String you pass in to format, like so

@Test
public void testDateConversion() {
    //also changed this from 23:50 to 19:50
    String strDate = "Aug 03, 2015, 19:50";
    Date date = parseDate(strDate, "MMM dd, yyyy, HH:mm");

    String dateFormatted = formatDate(date, "yyyy, MMM dd, HH:mm");
    assertEquals("2015, Aud 03, 19:50", dateFormatted); // Fails
} 

Otherwise your code is formatting as expected. The problem is, you're passing the date string in pre-formatted, so your end isn't going to change from what you started with. Your unit test is failing because you're passing in "Aug 03, 2015, 23:50" and telling Junit to expect "Aug 03, 2015, 19:50";. When you pass in a String to Date like that, the time value isn't going to change at all

AndrewSmiley
  • 1,933
  • 20
  • 32