how to convert timestamp string to java.util.Date

后端 未结 3 1892
星月不相逢
星月不相逢 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.

    0 讨论(0)
  • 2021-01-07 23:00

    use a SimpleDateFormat with an appropriate format string (be careful to use the correct format letters, uppercase and lowercase have different meanings!).

    0 讨论(0)
  • 2021-01-07 23:12

    tl;dr

    java.time.LocalDateTime.parse(
        "012318123456" ,
        DateTimeFormatter.ofPattern( "MMdduuHHmmss" )
    ).format( 
        DateTimeFormatter.ofPattern( "MM-dd-uu HH-mm-ss" )
    )
    

    01-23-18 12-34-56

    java.time

    The modern approach uses the java.time classes.

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

    Parse as a LocalDateTime because your input string lacks any indicator of time zone or offset-from-UTC.

    LocalDateTime ldt = LocalDateTime.parse( "012318123456" , f ) ;
    

    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

    ISO 8601

    Both of your formats are terrible, for multiple reasons.

    When serializing date-time values, use the standard ISO 8601 formats whenever possible. 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.

    2018-01-23T12:34:56


    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.

    Where to obtain the java.time classes?

    • 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 6 and 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 ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    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.

    0 讨论(0)
提交回复
热议问题