简体   繁体   中英

Formatting Date in Thread safe Manner throws java.text.ParseException:

I am trying to convert the date from one format to Another

Input String - 2012-11-07-121603 (yyyy-MM-dd-HH-mm-ss)

Output String - 2012-11-07-12:16:03:000 (yyyy-MM-dd-HH:mm:ss:SSS)

ThreadSafeSimpleDateFormatUtil simpleDateFormat = new ThreadSafeSimpleDateFormatUtil(GenericConstants.DATE_FORMAT.YYYY_MM_DD_HH_MM_SS.toString());
final Date parsedDate = simpleDateFormat.parse(bulkCollectionTime);
simpleDateFormat = new ThreadSafeSimpleDateFormatUtil(GenericConstants.DATE_FORMAT.YYYY_MM_DD_HH_MM_SS_SSS.toString());
writeBean.setTimestamp(simpleDateFormat.format(parsedDate));

But it's throwing below error:

java.text.ParseException: Unparseable date: "2012-11-07-121603"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at com.belgacom.rosy.rebecca.utils.ThreadSafeSimpleDateFormatUtil.parse(ThreadSafeSimpleDateFormatUtil.java:39)

看起来输入格式应该在小时,分钟和秒之间没有“ - ”:

2012-11-07-121603 (yyyy-MM-dd-HHmmss)
Input String - 2012-11-07-121603 (yyyy-MM-dd-HH-mm-ss)

错误的模式,它会与yyyy-MM-dd-HHmmss

tl;dr

LocalDateTime.parse( 
    "2012-11-07-121603" ,
    DateTimeFormatter.ofPattern( "uuu-MM-dd-HHmmss" )
).toString()

2012-11-07T12:16:03

java.time

The modern approach uses the java.time classes built into Java 8 and later. These classes are inherently thread-safe . These classes supplant the troublesome old legacy date-time classes that are un-thread-safe.

String input = "2012-11-07-121603" ; DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuu-MM-dd-HHmmss" ) ;

Parse as a LocalDateTime because your input lacks info about time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.parse( input , f );

ldt.toString(): 2012-11-07T12:16:03

Thread-safety

The java.time classes are designed to be thread-safe . The classes use immutable objects . You may cache objects such as DateTimeFormatter for re-use throughout your code and across threads.

ISO 8601

When serializing date-time values to text, use standard ISO 8601 formats only. Do not invent your own formats such as that shown in your Question. The standard formats are practical and sensible. They are designed to be easy to parse by machine yet easy to read by humans across cultures.

The java.time classes use ISO 8601 formats by default when parsing/generating strings. Example shown above.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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