Arpan Paliwal
Arpan Paliwal

Reputation: 264

Why is DateTimeFormatter is not printing date in specified format?

My code:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTime dt = formatter.parseDateTime("2014-04-24 18:22:01.867");
System.out.println("dt : "+dt);

Output:

dt : 2014-04-24T18:22:01.867+05:30

Expected output should be the same String:

dt : 2014-04-24 18:22:01.867

Upvotes: 4

Views: 238

Answers (1)

durron597
durron597

Reputation: 32323

The reason your output isn't the same thing is because you aren't using the formatter to do your printing. The formatter isn't "paired" with the DateTime, you have to explicitly use it.

Try this:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTime dt = formatter.parseDateTime("2014-04-24 18:22:01.867");
System.out.println("dt : " + fomatter.print(dt));

Upvotes: 2

Related Questions