Java 将 Joda-Time DateTime - ISO 8601 格式日期转换为另一种日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21505858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:03:26  来源:igfitidea点击:

Convert Joda-Time DateTime - ISO 8601 format date to another date format

javajodatime

提问by MChan

Inside my Java app I am using Joda-Timeto convert the app user entered date from MM/dd/yyyy to ISO 8601 format in order to save it in DB.

在我的 Java 应用程序中,我使用Joda-Time将应用程序用户输入的日期从 MM/dd/yyyy 转换为 ISO 8601 格式,以便将其保存在 DB 中。

Can someone please tell me how I can convert the ISO 8601date back to MM/dd/yyyy format using Joda-Time?

有人可以告诉我如何使用 Joda-Time将ISO 8601日期转换回 MM/dd/yyyy 格式吗?

My code convert user date to ISO 8601 date format:

我的代码将用户日期转换为 ISO 8601 日期格式:

String date1 = "05/05/2013";
DateTimeFormatter parser1 = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime dateTimeObj1 = DateTime.parse(date1,parser1);
DateTimeFormatter isoDateFormat = ISODateTimeFormat.dateTime();
String isoDateStr = isoDateFormat.print(dateTimeObj1);
System.out.println(isoDateStr);

采纳答案by Basil Bourque

Use Same Formatter

使用相同的格式化程序

You use the same DateTimeFormatterobject to parse as to print(render a string) in Joda-Time2.3.

您在Joda-Time2.3 中使用相同的 DateTimeFormatter对象来解析print(渲染字符串)。

Time Zone

时区

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.

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

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 表示日期和时间。仅解析日期部分的字符串时,时间部分会自动设置为当天的第一时刻。第一个时刻因时区而异。因此,应用不同的时区会产生不同的结果,沿着宇宙时间线的不同点,不同的毫秒数。

Note the call to withZonewhen defining the formatter.

请注意withZone定义格式化程序时的调用。

Strings

字符串

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

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

  • Call the toStringmethod on the DateTime instance.
    Every DateTime has a built-in ISO 8601formatter, used automatically by the "toString" method.
  • Instantiate your own DateTimeFormatterinstance.
  • 调用toStringDateTime 实例上的方法。
    每个 DateTime 都有一个内置的ISO 8601格式化程序,由“toString”方法自动使用。
  • 实例化您自己的DateTimeFormatter实例。

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

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

Example Code

示例代码

// 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 );

When run…

运行时…

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 DateTimehas no format. That class can parse a String in ISO 8601 format to instantiate a date-time object. Likewise a DateTimeFormattercan parse a String to instantiate a date-time object.

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

Going the opposite direction, a DateTimehas a toStringimplementation that generates a String representation of the date-time object's value. Likewise a DateTimeFormattercan generate a String representation of the date-time object's value.

相反,aDateTime有一个toString实现,它生成日期时间对象值的字符串表示。同样, aDateTimeFormatter可以生成日期时间对象值的字符串表示。

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

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