Illegal pattern character 'T' when parsing a date string to java.util.Date

后端 未结 3 465
野性不改
野性不改 2020-11-22 10:03

I have a date string and I want to parse it to normal date use the java Date API,the following is my code:

public static void main(String[] args) {
    Strin         


        
3条回答
  •  遥遥无期
    2020-11-22 10:44

    tl;dr

    Use java.time.Instant class to parse text in standard ISO 8601 format, representing a moment in UTC.

    Instant.parse( "2010-10-02T12:23:23Z" )
    

    ISO 8601

    That format is defined by the ISO 8601 standard for date-time string formats.

    Both:

    • java.time framework built into Java 8 and later (Tutorial)
    • Joda-Time library

    …use ISO 8601 formats by default for parsing and generating strings.

    You should generally avoid using the old java.util.Date/.Calendar & java.text.SimpleDateFormat classes as they are notoriously troublesome, confusing, and flawed. If required for interoperating, you can convert to and fro.

    java.time

    Built into Java 8 and later is the new java.time framework. Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.

    Instant instant = Instant.parse( "2010-10-02T12:23:23Z" );  // `Instant` is always in UTC.
    

    Convert to the old class.

    java.util.Date date = java.util.Date.from( instant );  // Pass an `Instant` to the `from` method.
    

    Time Zone

    If needed, you can assign a time zone.

    ZoneId zoneId = ZoneId.of( "America/Montreal" ); // Define a time zone rather than rely implicitly on JVM’s current default time zone.
    ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );  // Assign a time zone adjustment from UTC.
    

    Convert.

    java.util.Date date = java.util.Date.from( zdt.toInstant() );  // Extract an `Instant` from the `ZonedDateTime` to pass to the `from` method.
    

    Joda-Time

    UPDATE: The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.

    Here is some example code in Joda-Time 2.8.

    org.joda.time.DateTime dateTime_Utc = new DateTime( "2010-10-02T12:23:23Z" , DateTimeZone.UTC );  // Specifying a time zone to apply, rather than implicitly assigning the JVM’s current default.
    

    Convert to old class. Note that the assigned time zone is lost in conversion, as j.u.Date cannot be assigned a time zone.

    java.util.Date date = dateTime_Utc.toDate(); // The `toDate` method converts to old class.
    

    Time Zone

    If needed, you can assign a time zone.

    DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
    DateTime dateTime_Montreal = dateTime_Utc.withZone ( zone );
    


    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?

    • 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 (<26), 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.

提交回复
热议问题