0

I was parsing a datetime string using this: http://developer.android.com/reference/java/text/SimpleDateFormat.html but somehow i was getting runtime error on parsing: https://ideone.com/gpFqwp

Can anyone point me to my mistake here?

static String date = "2015-09-17T08:22:49Z";
public static DateFormat inputDatetimeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
public static DateFormat displayDateFormat = DateFormat.getDateTimeInstance();

public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(displayDateFormat.format(inputDatetimeFormatter.parse(date)));
}
solution
  • 115
  • 1
  • 12

2 Answers2

0

Change inputDatetimeFormatter to new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");. Then it will run fine.

EDIT : What is the purpose of the Z at the end of your date string? Is it just a letter that you always want to be there?

Then you will have to change inputDatetimeFormatter to the above. If the purpose of Z is for the time zone then your date string is wrong (shouldn't contain Z but a time zone, e.g. -0800).

Then it would give :

static String date = "2015-09-17T08:22:49-0800"; // Replace -0800 by wanted time zone
public static DateFormat inputDatetimeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // Now you can use Z (instead of 'Z') to indicate that the time zone is following
Kevin
  • 2,813
  • 3
  • 20
  • 30
0
DateFormat inputDatetimeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");

See javadoc for SimpleDateFormat in Java 7

or use

 DateFormat inputDatetimeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

See javadoc for SimpleDateFormat in Java 6

here is a sample

karim mohsen
  • 2,164
  • 1
  • 15
  • 19