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

前端 未结 8 2086
日久生厌
日久生厌 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

提交回复
热议问题