How to print datetime without milliseconds

I am reading data from a file and you will have to serialize it elsewhere after some calculations. How can I get the following to print without milliseconds so that the string that was passed to DateTime.parse and output is identical?

System.out.println(DateTime.parse("2015-06-06T01:51:49-06:00").toString()) 

2015-06-06T01: 51: 49.000-06: 00

+6
source share
3 answers
 DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateTimeNoMillis()).toFormatter().withOffsetParsed(); formatter.print(DateTime.parse("2015-06-06T01:51:49-06:00")) 
+3
source

You can use joda time formatting :

 DateTime dt = new DateTime(); DateTimeFormatter fmt = DateTimeFormat.forPattern("MMMM, yyyy"); String str = fmt.print(dt); 
+5
source

Take a look at this SimpleDateFormat.

+1
source

All Articles