简体   繁体   English

在java中以特定格式将日期格式化为字符串时,时间会发生变化

[英]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, 我有一个值为“Sat Nov 14 00:00:00 GMT + 05:30 2015”的日期。我需要将此变量转换为格式为“yyyy-MM-dd hh:mm:ss”的字符串日期。为此我使用下面的代码,

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? 其中reDate的值为“Sat Nov 14 00:00:00 GMT + 05:30 2015”。但转换后我得到的值为“2015-11-14 12:00:00”。时间“00:00: 00“变为”12:00:00“。怎么会发生这种情况? I need to get the exact time after conversion.That is the result as "2015-11-14 00:00:00".How to do that? 我需要在转换后获得确切的时间。这就是“2015-11-14 00:00:00”的结果。怎么做? Please help me to solve this. 请帮我解决这个问题。

You should be using HH instead of hh as mentioned here like: 您应该使用HH代替hh提到这里喜欢:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM