解析ISO 8601格式的日期时间字符串,不带UTC偏移量 [英] Parse a date-time string in ISO 8601 format without offset-from-UTC

查看:70
本文介绍了解析ISO 8601格式的日期时间字符串,不带UTC偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将字符串类型的日期解析为Date格式,但是每次都遇到此异常

I am parsing date of string type into Date format , but everytime got this exception

java.text.ParseException: Unparseable date: "2016-05-21T00:00:00" (at offset 4)

我的代码是:

   String d = "2016-05-21T00:00:00";
    DateFormat df = new SimpleDateFormat("yyyy MM dd HH:mm:ss", Locale.ENGLISH);
    Date myDate = null;
    try {
        myDate = df.parse(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }

推荐答案

您的日期字符串和DateFormat需要匹配.对于您输入的"2016-05-21T00:00:00" ,正确的DateFormat为:

Your date string and DateFormat need to match. For your input "2016-05-21T00:00:00", the correct DateFormat is:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

这篇关于解析ISO 8601格式的日期时间字符串,不带UTC偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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