KJEjava48 KJEjava48 - 1 year ago 58
Java Question

Time gets changed when formatting date to string in a particular format in java

I have a date with value "Sat Nov 14 00:00:00 GMT+05:30 2015".I need to convert this variable to a string date with format "yyyy-MM-dd hh:mm:ss".For that i used the below code,

SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date=dtf.format(reDate);

where reDate has the value "Sat Nov 14 00:00:00 GMT+05:30 2015".But after conversion i got the value as "2015-11-14 12:00:00".The time "00:00:00" get changed to "12:00:00".How that happens? I need to get the exact time after conversion.That is the result as "2015-11-14 00:00:00".How to do that? Please help me to solve this.

Answer Source

You should be using HH instead of hh as mentioned here like:

SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date=dtf.format(reDate);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download