-3

i am trying to convert original datetime in to my format but is give me exception

my date :2014-01-22 13:24:03 in this format now i want to convert

Date.java

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd k:mm:ss");
Date date = format.parse("2014-01-22 13:24:03");

String strdate = android.text.format.DateFormat.format("dd.MM kk:mm", date).toString();

i want output like this

22.1 13.24

Logcat Error



01-22 13:24:24.421: W/System.err(27821): java.text.ParseException: Unparseable date: "2014-01-22 13:24:03" (at offset 10)
    01-22 13:24:24.431: W/System.err(27821): at java.text.DateFormat.parse(DateFormat.java:626)
Mahesh
  • 1,559
  • 6
  • 27
  • 57

4 Answers4

1

Use this code

  try
    {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = format.parse("2014-01-22 13:24:03");
      String newdate = "" + date.getDate() + "." + date.getMonth() + " " + date.getHours() + "." + date.getMinutes(); 
    }
  catch( Exception e) {}
Tapa Save
  • 4,769
  • 5
  • 32
  • 54
0

You have a T in your pattern, but not in the date you're trying to parse. Get rid of the T in your pattern and the problem should go away.

From SimpleDateFormat documentation

"yyyy-MM-dd'T'HH:mm:ss.SSSZ" --> 2001-07-04T12:08:56.235-0700

Note the T in the pattern matches the corresponding T in the date string being parsed.

It's also a little odd that your pattern includes Kk, as upper and lowercase k have different meanings:

k Hour in day (1-24)

K Hour in am/pm (0-11)

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
0

The correct pattern for that date format is: yyyy-MM-dd' 'HH:mm:ss.

Then, in order to format the date the way you need, you can use this pattern: dd.M' 'HH.mm.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

Try do it in that way and review your formatting string.:)

String ISO_8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss");
SimpleDateFormat formatter = new SimpleDateFormat();
formatter.format(date);
RMachnik
  • 3,598
  • 1
  • 34
  • 51