如何将timestamp字符串转换为java.util.Date [英] how to convert timestamp string to java.util.Date
问题描述
我需要将时间戳字符串转换为 java.util.Date
。例如:
I need to convert a timestamp string to java.util.Date
. E.g.:
MMDDYYHHMMSS
到 MM-DD-YY HH-MM-SS
其中 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.
推荐答案
你可以这样做:
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");
但我强烈建议您使用 Joda Time 。这是一个更好的日期/时间库,很长很长的路要走。特别是,Joda Time中的格式化程序/解析器是线程安全的,因此您可以自由地和静态地重用它们; java.text.SimpleDateFormat
不是线程安全的,因此您需要为每个线程创建一个或使用<$序列化对它的访问权限c $ c>已同步阻止。
but I would strongly recommend that you use Joda Time instead. 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't thread-safe, so you either need to create one per thread or serialize access to it with a synchronized
block.
这篇关于如何将timestamp字符串转换为java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!