[英]Human readable and parsable date format in Java
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. 我尝试了很多东西,我能找到的最好的东西是使用DateFormater进行解析和格式化,但它有一个挫折。 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.
我试图找到是否有一个选项来格式化它并显示秒数(更好的是毫秒级别,因为那是Date对象允许的分辨率)但是我做得很短。
Any ideas? 有任何想法吗?
Take a look at java.text.SimpleDateFormat 看看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
SimpleDateFormat
可以基于包含秒和偶数毫秒的非常简单的模式系统来格式化和解析日期。
Other answers are all good. 其他答案都很好。
But when doing this kind of thing please pick a format that sorts properly when coded as a string.... "yyyy/MM/dd HH:mm:ss" is fine. 但是当做这种事情时, 请选择一种格式,当编码为字符串时可以正确排序 ....“yyyy / MM / dd HH:mm:ss”很好。 It always astounds me when software engineers pick a date format which doesn't sort in the obvious, convenient way.
当软件工程师选择一种不以明显,方便的方式排序的日期格式时,它总是令我惊讶。
You'll save your fellow developers a lot of pain at some distant point in the future - think of it as good karma :-) 你将在未来的某个遥远的地方拯救你的同事们很多痛苦 - 把它想象成好的业力:-)
A little off-topic, but I always feel the need to remind people that DateFormat and SimpleDateFormat are not thread safe! 有点偏离主题,但我总觉得有必要提醒人们DateFormat和SimpleDateFormat不是线程安全的! The Sun documentation clearly states this, but I keep finding code out in the wild where people stick a SimpleDateFormat in a static ...
Sun文档清楚地说明了这一点,但是我一直在野外找到代码,人们在静态中使用SimpleDateFormat ...
Use ISO 8601 format. 使用ISO 8601格式。
2009-10-22T21:13:14
, I should say it's pretty human-readable (though the T
in the middle that denotes the start of the time part may feel unusual at first). 2009-10-22T21:13:14
,我应该说这是非常人性化的(尽管中间的T
表示时间的开始部分可能在最初时感觉不寻常)。 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 their toString
methods. 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()
,所以这不应该让人感到惊讶。
如果你想做一点点简单,并且免于制作你自己的大多数其他Answers所涉及的DateFormat,你可以利用java.time.Instant中的默认格式:
(new Date()).toInstant.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.