How to convert string to date object?

前端 未结 5 2142
不知归路
不知归路 2020-12-22 03:47
String date=\"2006-06-21T15:57:24.000Z\";

How do I convert this String to a Date object without changing this format in Android?

相关标签:
5条回答
  • 2020-12-22 04:11

    tl;dr

    How do I convert this String to a Date object

    Date is supplanted by java.time.Instant.

    Instant.parse( "2006-06-21T15:57:24.000Z" )
    

    without changing this format

    Date-time objects do not have a “format”. Only text has a format.

    String output = instant.toString() ;  // Generate text in a `String` object in standard ISO 8601 format that represents the value of the `Instant` date-time object.
    

    ISO 8601

    That input string happens to be in standard ISO 8601 format. The Z on the end is short for Zulu and means UTC.

    java.time

    The java.time classes use ISO 8601 formats by default when parsing and generating strings that represent date-time values.

    The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

    Instant instant = Instant.parse( "2006-06-21T15:57:24.000Z" );
    

    To generate a String in standard ISO 8601 format, call toString.

    String output = instant.toString(); 
    

    2006-06-21T15:57:24Z

    String != date-time

    Do not conflate a date-time object with a String representing the value. The date-time object can parse a String, and can generate a String, but is not the String. In other words, a String can be input and/or output but is not the date-time object itself.

    So your question, “How do I convert this String to a Date object without changing this format” makes no sense.

    To generate a String in formats other than ISO 8601, convert your Instant to an OffsetDateTime or ZonedDateTime object, and use the DateTimeFormatter class. Search Stack Overflow for DateTimeFormatter to see more discussion and many examples.

    Conversion

    You should avoid the old java.util.Date class whenever possible. But if you must interface with old code not yet updated to the java.time types, you may convert to/from java.time via new methods added to the old date-time classes.

    java.util.Date utilDate = java.util.Date.from( instant );
    

    …and going the other direction…

    Instant instant = utilDate.toInstant();
    

    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, Java SE 10, Java SE 11, and later - 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
      • Most 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….
    0 讨论(0)
  • 2020-12-22 04:30

    I came here because this was supposedly an answered question of Covert RFC3339 DateTime to Date in java. However, Time is a deprecated class in Android since API level 22.

    A simple answer is based on this one:

    import com.google.api.client.util.DateTime;
    
    Date date = new Date(new DateTime("2006-06-21T15:57:24.000Z").getValue());
    
    0 讨论(0)
  • 2020-12-22 04:32

    See SimpleDateFormat, http://developer.android.com/reference/java/text/SimpleDateFormat.html

    This class converts Strings to Dates and vice versa, using a given pattern.

    Once you have created a SimpleDateFormat with the right pattern, you can use it to convert the string to a Date, use the date as you like, and eventually convert the Date back to a String using that same SimpleDateFormat instance.

    EDIT: clarification on time zones

    In the question it is not specified wether the given string is a "pure" ISO 8601 date, and in that case whether you need or not to support multiple time zones, if that timezones will be represented as only numbers (+0200 as in RFC 822), numbers with a colon (+02:00 as permitted by ISO 8601) or as names (EST etc...).

    In case the string is a pure ISO 8601 String, then SimpleDateFormat will have some problems decoding the time zone. If however it is "always Z" (meaning that timezone data is not meaningful and you can safely ignore it), or uses numbers without colon (like +0200 etc..), or uses time zone names, then SimpleDateFormat can handle it correctly.

    0 讨论(0)
  • 2020-12-22 04:33

    Here simple code for this:

      private Date parseDate(String date) {
        SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");
        Date dateObj = new Date();
        try {
          dateObj = curFormater.parse(date);
        } catch (ParseException e) {
          e.printStackTrace();
        }
        return dateObj;
      }
    
    0 讨论(0)
  • 2020-12-22 04:36

    Use the Time class and parse the string. Then use the Time toMillis() function and instantiate a Date.

    http://developer.android.com/reference/android/text/format/Time.html#parse3339(java.lang.String)

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