java Java中人类可读和可解析的日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1468444/
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
Human readable and parsable date format in Java
提问by Savvas Dalkitsis
I want to save a Date object to a readable string (for example 22/10/2009 21:13:14) that is also parsable back to a Date object.
我想将 Date 对象保存为可读字符串(例如 22/10/2009 21:13:14),该字符串也可解析回 Date 对象。
I have tried many things and the best I could find was to use DateFormater for parsing and formating but it has a setback. When you format a date you lose seconds information. I tried to find if there is an option to format it and display the seconds (even better would be to the millisecond level since that's the resolution the Date object allows you to have) but I came up short.
我尝试了很多东西,我能找到的最好的方法是使用 DateFormater 进行解析和格式化,但它有一个挫折。当您格式化日期时,您会丢失秒信息。我试图找到是否有一个选项可以对其进行格式化并显示秒数(最好是毫秒级别,因为这是 Date 对象允许您拥有的分辨率),但我没有找到。
Any ideas?
有任何想法吗?
回答by ChssPly76
Take a look at java.text.SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date dt = new Date();
String S = sdf.format(dt); // formats to 09/23/2009 13:53:28.238
Date dt2 = sdf.parse(S); // parses back
回答by Vincent Robert
SimpleDateFormatcan format and parse a date based on a very simple pattern system that include second and even milliseconds.
SimpleDateFormat可以基于非常简单的模式系统格式化和解析日期,包括秒甚至毫秒。
回答by mikera
Other answers are all good.
其他答案都很好。
But when doing this kind of thing pleasepick a format that sorts properlywhen coded as a string.... "yyyy/MM/dd HH:mm:ss" is fine. It always astounds me when software engineers pick a date format which doesn't sort in the obvious, convenient way.
但是在做这种事情时,请选择一种在编码为字符串时正确排序的格式......“yyyy/MM/dd HH:mm:ss”很好。当软件工程师选择一种不以明显、方便的方式排序的日期格式时,我总是感到震惊。
You'll save your fellow developers a lot of pain at some distant point in the future - think of it as good karma :-)
你会在未来的某个遥远的时刻为你的开发伙伴省去很多痛苦 - 把它看作是好的业力 :-)
回答by Ole V.V.
ISO 8601
ISO 8601
Use ISO 8601format.
使用ISO 8601格式。
- It's flexible, it includes seconds and fraction of second if there are any, but you may also leave them out if they are 0.
- It's standard, so more and more tools format and parse it. Great for serialization for storage or data interchange.
- It goes like
2009-10-22T21:13:14, I should say it's pretty human-readable (though theTin the middle that denotes the start of the time part may feel unusual at first). - The strings sort properly, as mikera requested in another answer, as long as the years are in the four-digit range from 1000 through 9999.
- The classes of
java.time, the modern Java date and time API, as well as those of Joda Time parse ISO 8601 as their default, that is, without any explicit formatter, and produce the same format from theirtoStringmethods.
- 它很灵活,它包括秒和秒的分数(如果有的话),但如果它们为 0,您也可以将它们排除在外。
- 它是标准的,因此越来越多的工具对其进行格式化和解析。非常适合用于存储或数据交换的序列化。
- 它就像
2009-10-22T21:13:14,我应该说它非常易于阅读(尽管T中间表示时间部分开始的部分起初可能会感觉不寻常)。 - 只要年份在 1000 到 9999 的四位数范围内,字符串就可以正确排序,正如 mikera 在另一个答案中所要求的那样。
- 的类
java.time、现代 Java 日期和时间 API 以及 Joda Time 的类将 ISO 8601 解析为它们的默认值,即没有任何显式格式化程序,并从它们的toString方法中生成相同的格式。
A modest demonstration of using java.time:
使用的适度演示java.time:
LocalDateTime dateTime = LocalDateTime.of(2009, 10, 22, 21, 13, 14);
String readableString = dateTime.toString();
System.out.println(readableString);
LocalDateTime parsedBack = LocalDateTime.parse(readableString);
System.out.println(parsedBack);
This prints two identical lines:
这将打印两条相同的行:
2009-10-22T21:13:14
2009-10-22T21:13:14
The latter System.out.println()call implicitly calls toString()once more, so this shouldn't surprise.
后一个System.out.println()调用隐式调用了toString()一次,所以这并不奇怪。
回答by Erik Tjernlund
A little off-topic, but I always feel the need to remind people that DateFormat and SimpleDateFormat are not thread safe! The Sun documentation clearly states this, but I keep finding code out in the wild where people stick a SimpleDateFormat in a static ...
有点跑题,但我总觉得有必要提醒大家,DateFormat 和 SimpleDateFormat 不是线程安全的!Sun 文档清楚地说明了这一点,但我一直在野外寻找代码,人们将 SimpleDateFormat 粘贴到静态...
回答by cellepo
If you want to do it a little simpler, and be spared from making your own DateFormat that most other Answers involve, you can leverage the default format in java.time.Instant:
如果您想让它更简单一点,并且不想制作大多数其他答案涉及的自己的 DateFormat,您可以利用java.time.Instant 中的默认格式:
(new Date()).toInstant.toString();

