得到今天的日期和时间作为字符串

时间:2016-03-30 06:45:21

标签: java calendar

我是Java的新手,并尝试使用Calendar对象来获取现在的日期和时间作为字符串。我特别沉溺于对象和对象的转换。

这是我需要的格式(作为字符串):    2016-03-30T14:21:00Z

如果我能够正确地获取日期和时间格式,我可以使用字符串,但我正在努力使用已弃用的方法。

感谢您的回复

2 个答案:

答案 0 :(得分:3)

您最好的选择是开始使用Java 8的新Time API(如果您不能使用Java 8,则使用JodaTime)

LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);

输出2016-03-30T17:51:38.639Z(当我测试时)

答案 1 :(得分:1)

使用此链接解决了我的问题:

http://beginnersbook.com/2013/05/current-date-time-in-java/

感谢您的回复,我还将研究Java 8的时间API

相关问题