Java date is not preserving milliseconds in conversion with simple date format

前端 未结 5 1644
死守一世寂寞
死守一世寂寞 2021-01-19 01:32

I\'m trying to convert the following string \"2012-04-13 04:08:42.794\" to a date type:

    SimpleDateFormat dateFormat = new SimpleDateFormat(\         


        
5条回答
  •  逝去的感伤
    2021-01-19 02:13

    tl;dr

    myPreparedStatetment.setObject( 
        … ,
        LocalDateTime.parse(
            "2012-04-13 04:08:42.794".replace( " " , "T" )
        )
    )
    

    Details

    The Answer by SJuan76 is correct: You are being fooled by the poorly-designed output of the Date::toString method. Instead, use java.time classes.

    java.time

    The modern approach uses the java.time classes that supplanted the troublesome old date-time classes such as Date/Calendar.

    First convert your input string to fully comply with the ISO 8601 standard. Replace the SPACE in the middle with a T.

    String input = "2012-04-13 04:08:42.794".replace( " " , "T" ) ;
    

    Parse as a LocalDateTime since your input lacks an indicator of offset-from-UTC or time zone.

    LocalDateTime ldt = LocalDateTime.parse( input ) ;
    

    ldt.toString(): 2012-04-13T04:08:42.794

    SQL

    Avoid the date-time related java.sql classes. They too are supplanted by the java.time classes. As of JDBC 4.2, you can directly exchange java.time objects with your database. So you can forget all about the java.sql.Date class and its terrible hacked design.

    myPreparedStatement.setObject( … , ldt ) ;
    

    And…

    LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
    

    Morals

    Moral of the story # 1: Use smart objects, not dumb strings.

    Moral of the story # 2: Use only java.time objects. Avoid legacy date-time classes.


    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.

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

    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.

提交回复
热议问题