简体   繁体   English

将DateTime对象转换为字符串

[英]Converting a DateTime Object to an String

I have a simple question. 我有一个简单的问题。 How can I convert an ISO-8601 Date to an String? 如何将ISO-8601日期转换为字符串? I'm using a Date with the following format: 2019-02-05T08:21:15.000+01:00 and want to convert this Date Object to an String. 我正在使用以下格式的Date: 2019-02-05T08:21:15.000+01:00并希望将此Date对象转换为字符串。 I tried the following: 我尝试了以下方法:

String startString = (String) jsonObjectMap2.get("created_on"); 
//startString = "2019-02-21T09:47:58.699004+00:00"`
DateTime start = ISODateTimeFormat.dateTimeParser().parseDateTime(startString);
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss.SSSZ+|-hh:mm"); 
String formatedStartString = dateFormat.format(start);`

But Iam getting the following error: 但是我得到以下错误:

java.lang.IllegalArgumentException: Cannot format given Object as a Date. java.lang.IllegalArgumentException:无法将给定的Object格式化为Date。

What is the corrrect form of the DateFormat? DateFormat的正确格式是什么? I hope you can help me, thank you in advance. 希望您能帮到我,谢谢。

为什么不在DateTime对象上使用toString()?

tl;dr tl; dr

java.time.OffsetDateTime.parse( "2019-02-21T09:47:58.699004+00:00" )

java.time java.time

What class is DateTime ? DateTime是什么类? If you are using Joda-Time, know that the Joda-Time project is now in maintenance-mode. 如果您使用的是Joda-Time,请知道Joda-Time项目现在处于维护模式。 Its creator, Stephen Colbourne, went on to lead JSR 310 and implement the java.time classes built into Java. 它的创建者Stephen Colbourne继续领导JSR 310并实现了Java中内置的java.time类。

No need for formatting pattern 无需格式化模式

Your input string is in standard ISO 8601 format. 您的输入字符串为标准ISO 8601格式。 The java.time classes use the ISO 8601 formats by default when parsing/generating strings. 解析/生成字符串时, java.time类默认使用ISO 8601格式。

String input = "2019-02-21T09:47:58.699004+00:00" ;  // Standard ISO 8601 format.

OffsetDateTime

Your input string indicates an offset-from-UTC but not a time zone. 您的输入字符串表示相对于UTC偏移量,而不是时区。 So the appropriate class to represent this value is OffsetDateTime . 因此,代表此值的合适类是OffsetDateTime

OffsetDateTime odt = OffsetDateTime.parse( input ) ;

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

As Thomas alluded to in the comments, DateTime.format() consumes a Date object, not a DateTime object; 正如Thomas在评论中提到的那样, DateTime.format()使用Date对象,而不是DateTime对象。 hence the IllegalArgumentException . 因此IllegalArgumentException Judging by the format of your date you could try parsing your startString using the in-built java.time.ZonedDateTime class: 看你的日期,你可以尝试解析您的格式startString使用内置java.time.ZonedDateTime类:

ZonedDateTime dateTime = ZonedDateTime.parse(startString) //it can parse ISO-8601 date-times

You could then format your dateTime object using java.time.DateTimeFormatter 's static ofPattern() method which takes a String pattern to apply to your date-time and returns the formatted date as a String: 然后,您可以使用java.time.DateTimeFormatter的static ofPattern()方法来格式化dateTime对象,该方法采用String模式应用于您的日期时间,并将格式化后的日期作为String返回:

String formattedDateTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SSSZ+|-hh:mm"));

EDIT 编辑

As Basil pointed out, the OP's DateTime object includes a time offset which is not necessarily a time zone offset--as is represented by the ZonedDateTime class. 正如Basil指出的那样,OP的DateTime对象包含一个时间偏移量,它不一定是时区偏移量,如ZonedDateTime类所表示。 Thus the most appropriate class to use in this scenario is OffsetDateTime which provides the same capabilities but is more suited for the given use case. 因此,在这种情况下最适合使用的类是OffsetDateTime ,它提供相同的功能,但更适合给定的用例。 See Basil Bourque's answer 参见Basil Bourque的答案

I am not certain that this will solve your problem, But mm should be uppercase ie MM, Because MM describes months while mm describes minutes. 我不确定这是否可以解决您的问题,但是mm应该是大写字母,即MM,因为MM描述了几个月,而mm描述了分钟。

see the following code: 请参阅以下代码:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ+|- 
    hh:mm");

I have found the following to work for me 我发现以下对我有用的东西

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[XXX]");
String formatted = zonedDateTime.format(dateTimeFormatter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM