简体   繁体   中英

Date Conversion not giving 24 hour format

I have a string ="02/07/2015 5:10 PM";

I need a string date with format as: 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 .

首先,您必须在模式中添加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 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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