如何在没有毫秒的情况下打印 DateTime

How to print DateTime without milliseconds

我正在从文件中读取数据,需要在经过一些计算后将其序列化到其他地方。如何在没有毫秒的情况下打印以下内容,以便传递给 DateTime.parse 的字符串和输出的内容相同?

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

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

您可以使用 joda time formatter:

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

看看这个 SimpleDateFormat

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