无法理解"YYYY-MM-DDTHH:MM:SS";日期格式 [英] Not able to understand "YYYY-MM-DDTHH:MM:SS" date format

查看:70
本文介绍了无法理解"YYYY-MM-DDTHH:MM:SS";日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析以下日期时间字符串

I am trying to parse following date time string

2018-01-30T23:59:59.000

我无法理解它是哪种标准格式,例如UTC或ISO_8601

I am not able to understand which standard format it is like UTC or ISO_8601

同时以以下方式进行解析:

while parsing in the following manner:

SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'HH:MM:SS:MS");
        Date date = null;
        try {
            date = sdf.parse("2018-01-30T23:59:59.000");
        } catch (ParseException e) {
            e.printStackTrace();
        }

但是它引发了以下异常:

But It is throwing following exception:

java.text.ParseException: Unparseable date: "2018-01-30T23:59:59.000"

感谢您的帮助.

推荐答案

    LocalDateTime dateTime = LocalDateTime.parse("2018-01-30T23:59:59.000");
    System.out.println(dateTime);

此打印:

2018-01-30T23:59:59

2018-01-30T23:59:59

您的字符串为ISO 8601格式.UTC或协调世界时不是一种格式,它是一种标准时间,用于定义我们各自时区中其余时间的使用时间.

Your string is in ISO 8601 format. UTC or Coordinated Universal Time is not a format, it is a standard time used to define the time the rest of use in our respective time zones.

您使用的日期时间类 SimpleDateFormat Date 早已过时,尤其是前者特别麻烦.我建议您改用 java.time (现代的Java日期和时间API).与之合作真是太好了.

The date-time classes you were using, SimpleDateFormat and Date, are long outdated and the former in particular notoriously troublesome. I recommend that you instead use java.time, the modern Java date and time API. It is so much nicer to work with.

LocalDateTime 是带有一天中的时间,没有时区或与UTC偏移的日期.它的一元参数 parse 方法解析ISO 8601,这就是为什么不需要显式格式化程序的原因.

A LocalDateTime is a date with time of day and without time zone or offset from UTC. Its one-argument parse method parses ISO 8601, which is why no explicit formatter is needed.

您的格式模式字符串有很多问题.这是为什么您应该在没有任何显式格式化程序的情况下欣赏上述解决方案的原因之一.出错的第一件事是:格式模式字符串的冒号:在秒和毫秒之间,而日期时间字符串的句点为..这就是为什么您会得到例外.

Your format pattern string has a number of issues to it. Which is one reason why you should appreciate the above solution without any explicit formatter. The first thing that goes wrong is: Your format pattern string has a colon, :, between seconds and milliseconds, whereas your date-time string has a dot, .. This is why you get the exception.

但是,解决此问题,您的代码将产生以下 Date :

However, fixing this, your code yields the following Date:

CET 2017年12月31日23:00:00

Sun Dec 31 23:00:00 CET 2017

与预期的日期有一个月的距离,而分钟和秒数也丢失了.因为:

It’s one month off from the expected, and the minutes and seconds are missing. Because:

  • 大写字母 YYYY 适用于基于周的年份,仅适用于周号.您需要年份的小写 yyyy .
  • 大写字母 DD 是一年中的某天.您需要在每月的某天使用小写的 dd .
  • 您正确使用了大写的 MM 表示月份.再尝试几分钟,将无法正常工作.也许您现在可以猜到:它是小写的 mm .
  • 毫不奇怪,您需要几秒钟的小写 ss .
  • 使用 MS 毫秒很有趣. SimpleDateFormat 将其作为月份的 M (我们已经有过两次),将大写的 S 用作毫秒.相反,您需要大写的 SSS 来表示三位数的毫秒.
  • Uppercase YYYY is for week-based year and only useful with a week number. You need lowercase yyyy for year.
  • Uppercase DD is for day of year. You need lowercase dd for day of month.
  • You correctly used uppercase MM for month. Trying the same again for minutes won’t work. Maybe you can guess by now: it’s lowercase mm.
  • Not surprising you need lowercase ss for seconds.
  • UsingMS for milliseconds is interesting. SimpleDateFormat takes it as M for month (which we’ve already had twice before) and uppercase S for millisecond. Instead you needed uppercase SSS for the three digits of milliseconds.
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Wikipedia article: ISO 8601
  • Wikipedia article: Coordinated Universal Time on UTC

这篇关于无法理解"YYYY-MM-DDTHH:MM:SS";日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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