how to convert timestamp string to java.util.Date

后端 未结 3 1893
星月不相逢
星月不相逢 2021-01-07 22:16

I need to convert a timestamp string to java.util.Date. E.g.:

MMDDYYHHMMSS to MM-DD-YY HH-MM-SS

Where MM

3条回答
  •  一向
    一向 (楼主)
    2021-01-07 22:53

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

提交回复
热议问题