Why doesn't DateTimeFormatter print the date in the specified format?

My code is:

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

The expected result should be the same String:

dt : 2014-04-24 18:22:01.867
+4
source share
1 answer

The reason your output is not the same thing is because you are not using a formatter to print. The formatter is not "paired" with DateTime, you must use it explicitly.

Try the following:

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));
+2
source

Source: https://habr.com/ru/post/1599369/


All Articles