2015-11-19 151 views
6

我正在讀取文件中的數據,並需要在計算後將其序列化到其他地方。如何獲得以下內容以毫秒爲單位進行打印,以便傳遞給DateTime.parse的字符串與輸出的內容完全相同?如何打印日期時間,毫秒毫秒

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

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

回答

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

Th答案是唯一一個真正回答原始問題的人(更不用說那在同一時間是非常優雅的)。不知道爲什麼其他答案的答案很多。 PS:更簡單的解決方案是調用yourDateTime.toString(ISODateTimeFormat.dateTimeNoMillis()); – vanomart

+1

感謝@vanomart提供最簡單的解決方案! –

5

可以使用joda time formatter

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