Tareq
Tareq

Reputation: 473

Date value converted from Calendar outputs different format than String

This is the code:

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateFormat.parse("2019-03-06 10:54:06 AM"));
    calendar.add(Calendar.HOUR_OF_DAY, 6);

    System.out.println("String Format: " + dateFormat.format(calendar.getTime()));
    System.out.println("Date Format: " + dateFormat.parse(dateFormat.format(calendar.getTime())));

The result is:

String Format: 2019-03-06 04:54:06 PM
Date Format: Wed Mar 06 16:54:06 BDT 2019

But the expected result is:

String Format: 2019-03-06 04:54:06 PM
Date Format: 2019-03-06 04:54:06 PM

Can you please tell my how to to that?

Upvotes: 0

Views: 25

Answers (0)

Related Questions