如何将时间戳字符串转换为 java.util.Date
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2318719/
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
how to convert timestamp string to java.util.Date
提问by Manu
I need to convert a timestamp string to java.util.Date
. E.g.:
我需要将时间戳字符串转换为java.util.Date
. 例如:
MMDDYYHHMMSS
to MM-DD-YY HH-MM-SS
MMDDYYHHMMSS
到 MM-DD-YY HH-MM-SS
Where MM
is month, DD
is date, YY
is year, HH
is hours, MM
is minutes and SS
is seconds.
其中MM
是月,DD
是日期,YY
是年,HH
是小时,MM
是分钟,SS
是秒。
采纳答案by Jon Skeet
You cando it like this:
你可以这样做:
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");
but I would stronglyrecommend that you use Joda Timeinstead. It's a better date/time library by a long, long way. In particular, the formatters/parsers in Joda Time are thread-safe, so you can reuse them freely and statically; java.text.SimpleDateFormat
isn'tthread-safe, so you either need to create one per thread or serialize access to it with a synchronized
block.
但我强烈建议您改用Joda Time。从长远来看,它是一个更好的日期/时间库。特别是,Joda Time 中的格式化程序/解析器是线程安全的,因此您可以自由且静态地重用它们;java.text.SimpleDateFormat
不是线程安全的,因此您需要为每个线程创建一个或使用synchronized
块序列化对它的访问。
回答by Michael Borgwardt
use a SimpleDateFormatwith an appropriate format string (be careful to use the correct format letters, uppercase and lowercase have different meanings!).
使用带有适当格式字符串的SimpleDateFormat(注意使用正确的格式字母,大写和小写有不同的含义!)。
回答by Basil Bourque
tl;dr
tl;博士
java.time.LocalDateTime.parse(
"012318123456" ,
DateTimeFormatter.ofPattern( "MMdduuHHmmss" )
).format(
DateTimeFormatter.ofPattern( "MM-dd-uu HH-mm-ss" )
)
01-23-18 12-34-56
01-23-18 12-34-56
java.time
时间
The modern approach uses the java.timeclasses.
现代方法使用java.time类。
Define a formatting pattern to match your input string.
定义格式模式以匹配您的输入字符串。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMdduuHHmmss" ) ;
Your two-digit year will be interpreted as being 21st century ( 20xx ).
您的两位数年份将被解释为 21 世纪 ( 20xx )。
Parse as a LocalDateTime
because your input string lacks any indicator of time zone or offset-from-UTC.
解析为 aLocalDateTime
因为您的输入字符串缺少任何时区或UTC偏移量的指示符。
LocalDateTime ldt = LocalDateTime.parse( "012318123456" , f ) ;
ldt.toString(): 2018-01-23T12:34:56
ldt.toString(): 2018-01-23T12:34:56
Generate a string in your desired format.
以您想要的格式生成一个字符串。
DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "MM-dd-uu HH-mm-ss" ) ;
String output = ldt.format( fOut );
01-23-18 12-34-56
01-23-18 12-34-56
ISO 8601
ISO 8601
Both of your formats are terrible, for multiple reasons.
由于多种原因,您的两种格式都很糟糕。
When serializing date-time values, use the standard ISO 8601formats whenever possible. They are designed to be practical, easy to parse by machine, easy to read by humans across cultures.
序列化日期时间值时,请尽可能使用标准ISO 8601格式。它们旨在实用,易于机器解析,易于跨文化的人类阅读。
For a date-time time such as yours, the T
in the middle separates the date portion from the time-of-day portion.
对于像您这样的日期时间时间,T
中间的 将日期部分与时间部分分开。
2018-01-23T12:34:56
2018-01-23T12:34:56
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。