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
1

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Why should that work if I want the date formatted in GMT-4?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.