Parsing a Youtube API Date in Java [duplicate]

余生长醉 提交于 2019-12-23 07:22:57

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15750904/parsing-a-youtube-api-date-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!