简体   繁体   English

如何将timestamp字符串转换为java.util.Date

[英]how to convert timestamp string to java.util.Date

I need to convert a timestamp string to java.util.Date . 我需要将时间戳字符串转换为java.util.Date Eg: 例如:

MMDDYYHHMMSS to MM-DD-YY HH-MM-SS MMDDYYHHMMSSMM-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是秒。

You can do it like this: 可以这样做:

DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");

but I would strongly recommend that you use Joda Time instead. 但我强烈建议你改用Joda Time 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; 特别是,Joda Time中的格式化程序/解析器是线程安全的,因此您可以自由地和静态地重用它们; java.text.SimpleDateFormat isn't thread-safe, so you either need to create one per thread or serialize access to it with a synchronized block. java.text.SimpleDateFormat 不是线程安全的,因此您需要为每个线程创建一个或使用synchronized块序列化对它的访问。

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 java.time

The modern approach uses the java.time classes. 现代方法使用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. 解析为LocalDateTime因为您的输入字符串缺少任何时区指示符或与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 8601 formats whenever possible. 序列化日期时间值时,请尽可能使用标准ISO 8601格式。 They are designed to be practical, easy to parse by machine, easy to read by humans across cultures. 它们设计实用,易于通过机器解析,易于人类跨文化阅读。

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.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

使用带有适当格式字符串的SimpleDateFormat (小心使用正确的格式字母,大写和小写有不同的含义!)。

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

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