What is this date format? 2011-08-12T20:17:46.384Z

前端 未结 8 2095
日久生厌
日久生厌 2020-11-22 16:56

I have the following date: 2011-08-12T20:17:46.384Z. What format is this? I\'m trying to parse it with Java 1.4 via DateFormat.getDateInstance().parse(

相关标签:
8条回答
  • 2020-11-22 17:25

    If you guys are looking for a solution for Android, you can use the following code to get the epoch seconds from the timestamp string.

    public static long timestampToEpochSeconds(String srcTimestamp) {
        long epoch = 0;
    
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                Instant instant = Instant.parse(srcTimestamp);
                epoch = instant.getEpochSecond();
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSSS'Z'", Locale.getDefault());
                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date date = sdf.parse(srcTimestamp);
                if (date != null) {
                    epoch = date.getTime() / 1000;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return epoch;
    }
    

    Sample input: 2019-10-15T05:51:31.537979Z

    Sample output: 1571128673

    0 讨论(0)
  • 2020-11-22 17:26

    Not sure about the Java parsing, but that's ISO8601: http://en.wikipedia.org/wiki/ISO_8601

    0 讨论(0)
  • 2020-11-22 17:26

    This technique translates java.util.Date to UTC format (or any other) and back again.

    Define a class like so:

    import java.util.Date;
    
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    public class UtcUtility {
    
    public static DateTimeFormatter UTC = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZoneUTC();
    
    
    public static Date parse(DateTimeFormatter dateTimeFormatter, String date) {
        return dateTimeFormatter.parseDateTime(date).toDate();
    }
    
    public static String format(DateTimeFormatter dateTimeFormatter, Date date) {
        return format(dateTimeFormatter, date.getTime());
    }
    
    private static String format(DateTimeFormatter dateTimeFormatter, long timeInMillis) {
        DateTime dateTime = new DateTime(timeInMillis);
        String formattedString = dateTimeFormatter.print(dateTime);
        return formattedString;
    }
    

    }

    Then use it like this:

    Date date = format(UTC, "2020-04-19T00:30:07.000Z")
    

    or

    String date = parse(UTC, new Date())
    

    You can also define other date formats if you require (not just UTC)

    0 讨论(0)
  • 2020-11-22 17:31

    tl;dr

    Standard ISO 8601 format is used by your input string.

    Instant.parse ( "2011-08-12T20:17:46.384Z" ) 
    

    ISO 8601

    This format is defined by the sensible practical standard, ISO 8601.

    The T separates the date portion from the time-of-day portion. The Z on the end means UTC (that is, an offset-from-UTC of zero hours-minutes-seconds). The Z is pronounced “Zulu”.

    java.time

    The old date-time classes bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. Avoid them.

    Instead, use the java.time framework built into Java 8 and later. The java.time classes supplant both the old date-time classes and the highly successful Joda-Time library.

    The java.time classes use ISO 8601 by default when parsing/generating textual representations of date-time values.

    The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds. That class can directly parse your input string without bothering to define a formatting pattern.

    Instant instant = Instant.parse ( "2011-08-12T20:17:46.384Z" ) ;
    


    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.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    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. Hibernate 5 & JPA 2.2 support java.time.

    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 brought 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 (26+) bundle implementations of the java.time classes.
      • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
        • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

    0 讨论(0)
  • 2020-11-22 17:33

    The T is just a literal to separate the date from the time, and the Z means "zero hour offset" also known as "Zulu time" (UTC). If your strings always have a "Z" you can use:

    SimpleDateFormat format = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    

    Or using Joda Time, you can use ISODateTimeFormat.dateTime().

    0 讨论(0)
  • 2020-11-22 17:44

    There are other ways to parse it rather than the first answer. To parse it:

    (1) If you want to grab information about date and time, you can parse it to a ZonedDatetime(since Java 8) or Date(old) object:

    // ZonedDateTime's default format requires a zone ID(like [Australia/Sydney]) in the end.
    // Here, we provide a format which can parse the string correctly.
    DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
    ZonedDateTime zdt = ZonedDateTime.parse("2011-08-12T20:17:46.384Z", dtf);
    

    or

    // 'T' is a literal.
    // 'X' is ISO Zone Offset[like +01, -08]; For UTC, it is interpreted as 'Z'(Zero) literal.
    String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
    
    // since no built-in format, we provides pattern directly.
    DateFormat df = new SimpleDateFormat(pattern);
    
    Date myDate = df.parse("2011-08-12T20:17:46.384Z");
    

    (2) If you don't care the date and time and just want to treat the information as a moment in nanoseconds, then you can use Instant:

    // The ISO format without zone ID is Instant's default.
    // There is no need to pass any format.
    Instant ins = Instant.parse("2011-08-12T20:17:46.384Z");
    
    0 讨论(0)
提交回复
热议问题