且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Joda-Time DateTime - ISO 8601格式日期转换为其他日期格式

更新时间:2023-02-03 22:58:49

使用相同的格式化程序



使用相同的DateTimeFormatter 对象解析 print (渲染字符串) ://www.joda.org/joda-time/> Joda-Time 2.3。

Use Same Formatter

You use the same DateTimeFormatter object to parse as to print (render a string) in Joda-Time 2.3.

请注意,您的代码忽略了时区。在这种情况下,您将获得JVM的默认时区。不是一个好习惯。

Note that your code neglected to address a time zone. In that case you get the JVM's default time zone. Not a good practice.

DateTime代表日期和时间。仅解析日期部分的字符串时,时间部分会自动设置为当天的第一时刻。第一时刻因时区而异。因此,应用不同的时区会产生不同的结果,沿着宇宙时间轴的不同点,不同的毫秒 - 从纪元开始。

A DateTime represents both a date and a time. When parsing a string for only the date portion, the time portion is automatically set to first moment of the day. That first moment varies by time zone. So applying a different time zone gives a different result, a different point along the timeline of the Universe, a different milliseconds-since-epoch.

请注意对 在定义格式化程序时使用区域

保留请注意,DateTime对象不是字符串。您可以通过以下任一方式生成DateTime中包含的日期时间信息的字符串表示:

Keep in mind that DateTime objects are not Strings. You can generate a string representation of the date-time information contained inside a DateTime by either:


  • 调用 toString DateTime实例上的方法。
    每个DateTime都有一个内置的 ISO 8601 格式化程序,由toString方法自动使用。

  • 实例化你自己的 DateTimeFormatter 实例。

  • Call the toString method on the DateTime instance.
    Every DateTime has a built-in ISO 8601 formatter, used automatically by the "toString" method.
  • Instantiate your own DateTimeFormatter instance.

这些字符串生成技术都可以在下面的示例代码中看到。

Both of these string-generation techniques are seen in the example code below.

// Usually better to specify a time zone than rely on default.
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Hong_Kong" );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy" ).withZone( timeZone );

// Parse string into a DateTime. Define the format.
String input = "05/05/2013";
DateTime dateTime = formatter.parseDateTime( input ); // Defaults to first moment of the day.

// Render date-time as an ISO 8601 string. The "toString" method on DateTime defaults to a built-in ISO 8601 formatter.
// A DateTime object is not itself a string. But a DateTime can generate a string by calling its "toString" method.
String iso8601String = dateTime.toString();

// Parse string into a DateTime. Passing to constructor conveniently uses the built-in ISO 8601 parser built into DateTime class.
DateTime dateTime2 = new DateTime( iso8601String, timeZone );

// Render date-time as a string in a particular format.
String output = formatter.print( dateTime2 );

您可以对本地化格式进行软编码,而不是对特定格式进行硬编码。

Rather than hard-code a specific format, you can soft-code a localized format.

String outputUS = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.US ).print( dateTime2 );
String outputQuébécois = DateTimeFormat.forStyle( "F-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime2 );

转储到控制台......

Dump to console…

System.out.println( "dateTime: " + dateTime ); // Implicit call to "toString" method in DateTime class generates a new string using a built-in formatter for ISO 8601 format.
System.out.println( "iso8601String: " + iso8601String );
System.out.println( "dateTime2: " + dateTime2 ); // Another implicit call to "toString" method on DateTime class. Generates a new string in ISO format.
System.out.println( "output: " + output );

运行时...

dateTime: 2013-05-05T00:00:00.000+08:00
iso8601String: 2013-05-05T00:00:00.000+08:00
dateTime2: 2013-05-05T00:00:00.000+08:00
output: 05/05/2013



字符串不是日期时间



不要将日期时间对象视为字符串。

String Is Not a Date-Time

Do not think of date-time objects as strings.

A DateTime 没有格式。该类可以解析ISO 8601格式的String以实例化日期时间对象。同样, DateTimeFormatter 可以解析String以实例化日期时间对象。

A DateTime has no format. That class can parse a String in ISO 8601 format to instantiate a date-time object. Likewise a DateTimeFormatter can parse a String to instantiate a date-time object.

相反的方向, DateTime 有一个 toString 实现,生成日期时间对象值的String表示。同样, DateTimeFormatter 可以生成日期时间对象值的String表示。

Going the opposite direction, a DateTime has a toString implementation that generates a String representation of the date-time object’s value. Likewise a DateTimeFormatter can generate a String representation of the date-time object’s value.

在所有这些情况下,字符串表示完全不同,与日期时间对象分开。

In all these cases the String representation is entirely different and separate from the date-time object.