简体   繁体   English

日期转换未提供24小时格式

[英]Date Conversion not giving 24 hour format

I have a string ="02/07/2015 5:10 PM"; 我有一个string ="02/07/2015 5:10 PM";

I need a string date with format as: 2015-02-07 17:10:00; 我需要一个字符串日期,其格式为: 2015-02-07 17:10:00;

How can i achieve this? 我怎样才能做到这一点?

I have tried with: 我尝试过:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date datepicker1 = dateFormat.parse(datetime1);

Then 然后

SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy' 'HH:mm:ss");
String datetimepicker1 = dateFormat1.format(datepicker1);

But finally I get is: 2015-02-08 05:10:00 and I need 2015-02-08 17:10:00 . 但最后我得到的是: 2015-02-08 05:10:00我需要2015-02-08 17:10:00

首先,您必须在模式中添加a字母(代表AM/PM )并将HH更改为h

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy h:mm a");
String dateStr = "02/07/2015 5:10 PM";
DateFormat readFormat = new SimpleDateFormat( "dd/MM/yyyy hh:mm aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try
{
    date = readFormat.parse( dateStr );
}
catch ( ParseException e )
{
        e.printStackTrace();
}
if( date != null )
{
    String formattedDate = writeFormat.format( date );
}

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

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