0

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?

Tareq
  • 443
  • 1
  • 6
  • 21
  • 2
    We are in **2019**, so please use the "new" `java.time` api introduced in **2014** with Java 8, and not this old broken mess – Lino Mar 06 '19 at 14:21
  • 1
    You are asking the impossible. A `Date` hasn’t got (as in cannot have) a format. And 100 % what @Lino said so well. [Oracle tutorial: Date Time](https://docs.oracle.com/javase/tutorial/datetime/) explaining how to use java.time. – Ole V.V. Mar 06 '19 at 14:47

0 Answers0