sjain
sjain

Reputation: 23354

Correct date formatting for date strings like "2013-11-16T08:46:00.000-06:00"

INPUT:

SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

String unformattedDate = "2013-11-16T08:46:00.000-06:00";

String formattedDate = outFormat.format(inFormat.parse(unformattedDate ));

ABOVE OUTPUT:

formattedDate = "2013-11-16T20:16:00.000Z"

DESIRED OUTPUT:

formattedDate = "2013-11-16T08:46:00.000Z"

Can anyone through a light on why is this difference after conversion and how to get the desired output?

I assume that my inFormat is not correct to format the date unformattedDate.

Upvotes: 2

Views: 340

Answers (1)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

do like this

 String unformattedDate = "2013-11-16T08:46:00.000-06:00";
 Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(unformattedDate);
 String formattedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(date);
 System.out.println(formattedDate);

output

2013-11-16T08:46:00.000Z

Upvotes: 2

Related Questions