0

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.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Uday Khatry
  • 449
  • 1
  • 8
  • 23
  • possible duplicate of [converting date time to 24 hour format](http://stackoverflow.com/questions/6842245/converting-date-time-to-24-hour-format) – Basil Bourque Feb 04 '15 at 19:25

2 Answers2

2

In the beginning, you have to add the a letter to the pattern (which stands for AM/PM) and change HH to h:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy h:mm a");
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0
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 );
}
vipluv
  • 607
  • 5
  • 8