Nithinlal
Nithinlal

Reputation: 5061

Issue in SimpleDateFormat in android

I have the code to change time format

String date = "13/08/13 05.57 PM";

SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy hh.mm a");
Date testDate = null;
  try {
       testDate = sdf.parse(date);
      }catch(Exception ex){
       ex.printStackTrace();
      }
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);

I got the output as

 .....Date...13-57-2013 05:57:00 PM

instead of 13-08-2013 05:57:00 PM. Please help me to solve this issue.

Upvotes: 1

Views: 116

Answers (2)

Pankaj Kumar
Pankaj Kumar

Reputation: 83028

Problem with mm that should be use for minutes. For months you should use MM. Read more about Date and Time Patterns in Java


Use below code

String date = "13/08/13 05.57 PM";

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
        Date testDate = null;
          try {
               testDate = sdf.parse(date);
              }catch(Exception ex){
               ex.printStackTrace();
              }
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
        String newFormat = formatter.format(testDate);
        System.out.println(".....Date..."+newFormat);

Upvotes: 4

Rajesh
Rajesh

Reputation: 15774

Use 'MM' instead of 'mm' where month is expected. Take a look at Date and Time Formats for the explanation of the different format characters.

Upvotes: 2

Related Questions