Question

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)
Was it helpful?

Solution

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) {}

OTHER TIPS

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);

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)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top