Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a Youtube API Date in Java [duplicate]

Tags:

java

youtube

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

like image 816
Serhii Bohutskyi Avatar asked Apr 01 '13 20:04

Serhii Bohutskyi


1 Answers

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.

like image 135
spa Avatar answered Nov 16 '22 00:11

spa