-5

My input is 2014-12-12T10:39:40Z. I want output like 12//11/2014 8:58:15 AM. here i am using

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy'T' HH:mm:ss 'a'");

But i am geting parsing exception. let me know how to write simpledateformat.

Advance Thanks Srinivas B

jmj
  • 237,923
  • 42
  • 401
  • 438
Srinivas B
  • 31
  • 3

5 Answers5

1

you need two DateFormatter one to match your input pattern then second one to format it in the format you need, You are parsing input with your output SimpleDateFormat

jmj
  • 237,923
  • 42
  • 401
  • 438
1

If your input date is a String, then first you need to parse that to convert to a Date. You can use a SimpleDateFormat object for that.

Then, to output in the format you want, you need another SimpleDateFormat object.

Something like this:

    String input = "2014-12-12T10:39:40Z";
    SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    Date date = parser.parse(input);
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy H:mm:ss a");
    System.out.println(formatter.format(date));
janos
  • 120,954
  • 29
  • 226
  • 236
1

You're input appears to be ISO8601, so you can use DatatypeConverter. There are issues parsing 8601 dates using SimpleDateFormat, see Converting ISO 8601-compliant String to java.util.Date

SimpleDateFormat output = new SimpleDateFormat(
        "dd/MM/yyyy'T' HH:mm:ss 'a'");
Date input = javax.xml.bind.DatatypeConverter.parseDateTime(
        "2014-12-12T10:39:40Z").getTime();

System.out.println(output.format(input));

Java 8 using new time API

LocalDateTime localDateTime = LocalDateTime.parse("2014-12-12T10:39:40Z",
        DateTimeFormatter.ISO_DATE_TIME);

Date utilDate =  Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println(output.format(utilDate));
Community
  • 1
  • 1
Adam
  • 35,919
  • 9
  • 100
  • 137
1
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy'T' HH:mm:ss a");
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");

System.out.println(formatter.format(formatter1.parse("2014-12-12T10:39:40'a'")));
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
1

Try this:

String input = "2014-12-12T10:39:40Z";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aaa");
System.out.println(formatter.format(date));
Jens
  • 67,715
  • 15
  • 98
  • 113