7

I am reading data from a file and will need to serialize it out elsewhere after some computation. How can I get the following to print without milliseconds so that the string that is passed to DateTime.parse and what is outputted are identical?

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

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

user782220
  • 10,677
  • 21
  • 72
  • 135

3 Answers3

5
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateTimeNoMillis()).toFormatter().withOffsetParsed();
formatter.print(DateTime.parse("2015-06-06T01:51:49-06:00"))
user782220
  • 10,677
  • 21
  • 72
  • 135
  • This answer is the only one that actually answers the original questions (not to mention that is very elegant in the same time). Not sure why the other answers are upvoted that much. PS: Even simpler solution is to call yourDateTime.toString(ISODateTimeFormat.dateTimeNoMillis()); – vanomart Dec 12 '16 at 09:11
  • 1
    Thanks @vanomart for the simplest solution! – Sarneet Kaur Feb 09 '18 at 01:53
4

You can use the joda time formatter:

DateTime dt = new DateTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("MMMM, yyyy");
String str = fmt.print(dt);
shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Have a look at this SimpleDateFormat

Balakumar
  • 650
  • 1
  • 12
  • 29