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

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

提交回复
热议问题