10

What is the format of uploaded date of youtube api I can use in SimpleDateFormat?

example "2013-03-31T16:46:38.000Z"

P.S. solution was found yyyy-MM-dd'T'HH:mm:ss.SSSX

thanks

Serhii Bohutskyi
  • 2,261
  • 2
  • 28
  • 28
  • SimpleDateFormat will be able to parse just about any date string you throw at it. You only have to tell it the format in which to expect it. Also, Apache Commons Lang has some Date parsing abilities, I believe, that might simplify things. – CodeChimp Apr 01 '13 at 20:08
  • I also thought so, but java.text.ParseException: Unparseable date: "2013-03-31T16:46:38.000Z" – Serhii Bohutskyi Apr 01 '13 at 20:15

1 Answers1

3

It is a ISO 8061 date time

Actually, at least in Java8 it's very simple to parse that one as there is a predefined DateTimeFormatter. Here a small unit test as demonstration:

import org.junit.Test;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import static org.junit.Assert.assertEquals;

public class DateTimeFormatterTest {

    @Test
    public void testIsoDateTimeParse() throws Exception {
        // when
        final ZonedDateTime dateTime = ZonedDateTime.parse("2013-03-31T16:46:38.000Z", DateTimeFormatter.ISO_DATE_TIME);

        // then
        assertEquals(2013, dateTime.getYear());
        assertEquals(3, dateTime.getMonthValue());
        assertEquals(31, dateTime.getDayOfMonth());
        assertEquals(16, dateTime.getHour());
        assertEquals(46, dateTime.getMinute());
        assertEquals(38, dateTime.getSecond());
        assertEquals(ZoneOffset.UTC, dateTime.getZone());
    }
}

Prior to Java8, I would take a look at Converting ISO 8601-compliant String to java.util.Date and definitley default to using Joda Time with sth. like:

final org.joda.time.DateTime dateTime = new org.joda.time.DateTime.parse("2013-03-31T16:46:38.000Z");

BTW, don't use new DateTime("2013-03-31T16:46:38.000Z") as it will use your default time zone, which is probably not what you want.

Community
  • 1
  • 1
spa
  • 5,059
  • 1
  • 35
  • 59