用 Java 解析 Youtube API 日期 [英] Parsing a Youtube API Date in Java

查看:35
本文介绍了用 Java 解析 Youtube API 日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 SimpleDateFormat 中使用的 youtube api 上传日期的格式是什么?

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

示例2013-03-31T16:46:38.000Z"

附言找到解决方案yyyy-MM-dd'T'HH:mm:ss.SSSX

谢谢

推荐答案

It is a ISO 8061 日期时间

实际上,至少在 Java8 中,解析它非常简单,因为有一个预定义的 DateTimeFormatter.这里有一个小的单元测试作为演示:

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());
    }
}

在 Java8 之前,我会看看 Converting符合 ISO 8601 的字符串到 java.util.Date 和 definitley 默认使用 Joda Time 与某事.喜欢:

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");

顺便说一句,不要使用 new DateTime("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.

这篇关于用 Java 解析 Youtube API 日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆