简体   繁体   English

Java将日期转换为其他格式

[英]Java converting a Date to a different format

I have a date string in this format: 我有以下格式的日期字符串:

String fieldAsString = "11/26/2011 14:47:31";

I am trying to convert it to a Date type object in this format: "yyyy.MM.dd HH:mm:ss" 我正在尝试将其转换为以下格式的Date类型对象: “ yyyy.MM.dd HH:mm:ss”

I tried using the following code: 我尝试使用以下代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date newFormat = sdf.parse(fieldAsString);

However, this throws an exception that it is an Unparsable date. 但是,这引发了一个例外,那就是不可解析的日期。

So I tried something like this: 所以我尝试了这样的事情:

Date date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(fieldAsString);
String newFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date)

However, this new format is now in the 'String' format but I want my function to return the new formatted date as a 'Date' object type. 但是,这种新格式现在是“字符串”格式,但是我希望我的函数以“日期”对象类型返回新的格式化日期。 How would I do this? 我该怎么做?

Thanks! 谢谢!

You seem to be under the impression that a Date object has a format. 您似乎对Date对象具有格式有一种印象。 It doesn't. 没有。 It sounds like you just need this: 听起来您只需要这样:

Date date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(fieldAsString);

(You should consider specifying a locale and possibly a time zone, mind you.) (请注意,您应该考虑指定语言环境和时区。)

Then you've got your Date value. 然后,您就有了Date值。 A format is only relevant when you later want to convert it to text... that's when you should specify the format. 格式仅在以后需要将其转换为文本时才有意义... 那是您应该指定格式的时候。 It's important to separate the value being represent (an instant in time, in this case) from a potential textual representation. 将表示的值(在这种情况下为即时)与潜在的文本表示分开很重要。 It's like integers - there's no difference between these two values: 就像整数-这两个值之间没有区别:

int x = 0x10;
int y = 16;

They're the same value, just represented differently in source code. 它们是相同的值,只是源代码中的表示方式不同。

Additionally consider using Joda Time for all your date/time work - it's a much cleaner API than java.util.* . 此外,考虑将Joda Time用于所有日期/时间工作-与java.util.*相比,它是一种更加简洁的API。

The answer by Jon Skeet is correct and complete. 乔恩·斯基特(Jon Skeet )的回答是正确而完整的。

Internal to java.util.Date (and Date-Time seen below), the date-time value is stored as milliseconds since the Unix epoch . 在java.util.Date内部(和Date-Time如下所示),日期时间值以Unix 纪元以来的毫秒数存储。 There is no String inside! 里面没有弦! When you need a textual representation of the date-time in a format readable by a human, either call toString or use a formatter object to create a String object. 当需要以人类可读的格式表示日期时间的文本表示时,请调用toString或使用格式化程序对象创建 String对象。 Likewise when parsing, the input string is thrown away, not stored inside the Date object (or DateTime object in Joda-Time). 同样,在解析时,输入字符串也将被丢弃,而不是存储在Date对象(或Joda-Time中的DateTime对象)内部。

Joda-Time 乔达时代

For fun, here is the (better) way to do this work with Joda-Time , as mentioned by Mr. Skeet. 有趣的是,这是Skeet先生提到的与Joda-Time一起完成这项工作的(更好)方法。

One major difference is that while a java.util.Date class seems to have a time zone, it does not. 一个主要的区别是,尽管java.util.Date类似乎具有时区,但没有。 A Joda-Time DateTime in contrast does truly know its own time zone. 相反,Joda-Time DateTime确实知道自己的时区。

String input = "11/26/2011 14:47:31";

// From text to date-time.
DateTimeZone timeZone = DateTimeZone.forID( "Pacific/Honolulu" ); // Time zone intended but unrecorded by the input string.
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "MM/dd/yyyy HH:mm:ss" ).withZone( timeZone );
// No words in the input, so no need for a specific Locale.
DateTime dateTime = formatterInput.parseDateTime( input );

// From date-time to text.
DateTimeFormatter formatterOutput_MontréalEnFrançais = DateTimeFormat.forStyle( "FS" ).withLocale( java.util.Locale.CANADA_FRENCH ).withZone( DateTimeZone.forID( "America/Montreal" ) );
String output = formatterOutput_MontréalEnFrançais.print( dateTime );

Dump to console… 转储到控制台...

System.out.println( "input: " + input );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime as milliseconds since Unix epoch: " + dateTime.getMillis() );
System.out.println( "dateTime in UTC: " + dateTime.withZone( DateTimeZone.UTC ) );
System.out.println( "output: " + output );

When run… 运行时...

input: 11/26/2011 14:47:31
dateTime: 2011-11-26T14:47:31.000-10:00
dateTime as milliseconds since Unix epoch: 1322354851000
dateTime in UTC: 2011-11-27T00:47:31.000Z
output: samedi 26 novembre 2011 19:47

Search StackOverflow for "joda" to find many more examples. 在StackOverflow中搜索“ joda”以找到更多示例。

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

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