在我的Java应用程序中,我使用Joda-Time将应用程序用户输入的日期从MM/dd/yyyy转换为ISO 8601格式,以便将其保存在数据库中.
有人可以告诉我如何使用Joda-Time 将ISO 8601日期转换回MM/dd/yyyy格式吗?
我的代码将用户日期转换为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);
Basil Bourqu.. 23
使用相同的Formatter
您使用相同的DateTimeFormatter对象来解析Joda-Time 2.3中的print
(呈现字符串).
请注意,您的代码忽略了解决时区问题.在这种情况下,您将获得JVM的默认时区.不是一个好习惯.
DateTime表示日期和时间.仅解析日期部分的字符串时,时间部分会自动设置为当天的第一时刻.第一时刻因时区而异.因此,应用不同的时区会产生不同的结果,沿宇宙时间轴的不同点,不同的毫秒 - 自纪元.
请注意withZone
定义格式化程序时的调用.
请记住,DateTime对象不是字符串.您可以通过以下任一方式生成DateTime中包含的日期时间信息的字符串表示:
toString
在DateTime实例上调用该方法.
每个DateTime都有一个内置的ISO 8601 格式化程序,由"toString"方法自动使用.
实例化您自己的DateTimeFormatter
实例.
这两种字符串生成技术都可以在下面的示例代码中看到.
示例代码// 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 );
您可以对本地化格式进行软编码,而不是对特定格式进行硬编码.
String outputUS = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.US ).print( dateTime2 ); String outputQuébécois = DateTimeFormat.forStyle( "F-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime2 );
转储到控制台......
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
字符串不是日期时间
不要将日期时间对象视为字符串.
A DateTime
没有格式.该类可以解析ISO 8601格式的String以实例化日期时间对象.同样,a DateTimeFormatter
可以解析String以实例化日期时间对象.
相反的方向,a DateTime
有一个toString
实现,它生成一个日期时间对象值的String表示.同样,a DateTimeFormatter
可以生成日期时间对象值的String表示.
在所有这些情况下,String表示完全不同,并与日期时间对象分开.
您使用相同的DateTimeFormatter对象来解析Joda-Time 2.3中的print
(呈现字符串).
请注意,您的代码忽略了解决时区问题.在这种情况下,您将获得JVM的默认时区.不是一个好习惯.
DateTime表示日期和时间.仅解析日期部分的字符串时,时间部分会自动设置为当天的第一时刻.第一时刻因时区而异.因此,应用不同的时区会产生不同的结果,沿宇宙时间轴的不同点,不同的毫秒 - 自纪元.
请注意withZone
定义格式化程序时的调用.
请记住,DateTime对象不是字符串.您可以通过以下任一方式生成DateTime中包含的日期时间信息的字符串表示:
toString
在DateTime实例上调用该方法.
每个DateTime都有一个内置的ISO 8601 格式化程序,由"toString"方法自动使用.
实例化您自己的DateTimeFormatter
实例.
这两种字符串生成技术都可以在下面的示例代码中看到.
// 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 );
您可以对本地化格式进行软编码,而不是对特定格式进行硬编码.
String outputUS = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.US ).print( dateTime2 ); String outputQuébécois = DateTimeFormat.forStyle( "F-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime2 );
转储到控制台......
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
不要将日期时间对象视为字符串.
A DateTime
没有格式.该类可以解析ISO 8601格式的String以实例化日期时间对象.同样,a DateTimeFormatter
可以解析String以实例化日期时间对象.
相反的方向,a DateTime
有一个toString
实现,它生成一个日期时间对象值的String表示.同样,a DateTimeFormatter
可以生成日期时间对象值的String表示.
在所有这些情况下,String表示完全不同,并与日期时间对象分开.