简体   繁体   English

java.text.ParseException:无法解析的日期:“2014 年 5 月 13 日星期二 23:58:30 IST”

[英]java.text.ParseException: Unparseable date: "Tue May 13 23:58:30 IST 2014"

I am converting date format but instead getting error.我正在转换日期格式,但出现错误。 I know that the question is repeated the solution was not helping: Below is my code and the date field I am converting is in format: "2014-05-13T23:58:30.457"我知道问题重复了,解决方案没有帮助:下面是我的代码,我正在转换的日期字段的格式为:“2014-05-13T23:58:30.457”

val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.US)
val format2 = new SimpleDateFormat("yyyy-MM",Locale.US)

val result = data.filter{line=>{line.trim().startsWith("<row")}}
    .filter{line=>{line.contains("PostTypeId=\"1\"")}}
    .flatMap { line=>{
      val xml = XML.loadString(line)
      xml.attribute("CreationDate")
    }}.map{line=>{
      (format2.parse(format.parse(line.toString()).toString()),1)
    }}.reduceByKey(_+_)

And I am getting below error:我收到以下错误:

java.text.ParseException: Unparseable date: "Tue May 13 23:58:30 IST 2014"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at StackOverflowAnalysis.TotalQues$$anonfun$5.apply(TotalQues.scala:23)
    at StackOverflowAnalysis.TotalQues$$anonfun$5.apply(TotalQues.scala:22)

You should change format2.parse into format2.format .您应该将format2.parse更改为format2.format

When using .parse , the defined format of SimpleDateFormat should be exactly the same as that of the String.使用.parseSimpleDateFormat定义的格式应与 String 完全相同。

ps Special case: When using Restful API service, if there are differences between the backend server and local environment, the problem would show up as well. ps 特例:使用Restful API服务时,如果后端服务器和本地环境有差异,也会出现这个问题。

(Additional info translated from Chinese website: https://vimsky.com/article/2736.html ) (附加信息翻译自中文网站: https : //vimsky.com/article/2736.html

    String line = "2014-05-13T23:58:30.457";
    String reformatted = YearMonth.from(LocalDateTime.parse(line)).toString();
    System.out.println(reformatted);

This prints这打印

2014-05 2014-05

No use for specifying any formatters explicitly.没有用于明确指定任何格式化程序。 Your field is in ISO 8601 format, the format that LocalDateTime and the other classes in java.time parse as their default.您的字段采用 ISO 8601 格式, LocalDateTime和 java.time 中的其他类将其解析为默认格式。 java.time is the modern Java date and time API. java.time 是现代 Java 日期和时间 API。 YearMonth is a month in the calendar, as the class name says, a year and a month. YearMonth是日历中的一个月份,正如类名所说,一年和一个月。 Just what you need.正是您所需要的。 And its toString method formats it into your desired format of yyyy-MM , which conforms with ISO 8601 too.它的toString方法将其格式化为您想要的yyyy-MM格式,这也符合 ISO 8601。

The outdated and the modern date and time classes过时的和现代的日期和时间类

The classes you were using, SimpleDateFormat and implicitly Date , are long outdated and the former in particular notoriously troublesome.您使用的类SimpleDateFormat和隐式Date已经过时了,尤其是前者非常麻烦。 I recommend you avoid them.我建议你避免使用它们。 The modern API is so much nicer to work with.现代 API 使用起来要好得多。

What went wrong in your code你的代码出了什么问题

  • To parse means to analyse a string in order to determine the meaning of its contents.解析意味着分析字符串以确定其内容的含义。 Here parsing a date-time string converts the string to a date-time object ( Date or LocalDateTime ).这里解析日期时间字符串会将字符串转换为日期时间对象( DateLocalDateTime )。
  • To format means the opposite: to convert some data, here a date-time object, into a string, typically for human readability or for data transmission.格式化意味着相反:将一些数据(这里是日期时间对象)转换为字符串,通常用于人类可读性或数据传输。

So your code took your string of 2014-05-13T23:58:30.457 .因此,您的代码采用了2014-05-13T23:58:30.457字符串。 The toString call may be superfluous and just return the same string again. toString调用可能是多余的,只是再次返回相同的字符串。 Then it parses it into a Date object, this works nicely.然后它将它解析为一个Date对象,这很好用。 Calling toString on the Date formats it into the string Tue May 13 23:58:30 IST 2014 , which is what you didn't want to do.Date上调用toString会将其格式化为字符串Tue May 13 23:58:30 IST 2014 ,这是您不想做的。 Then you tried to parse this string into a new Date object using the format yyyy-MM .然后,您尝试使用yyyy-MM格式将此字符串解析为新的Date对象。 Since the string didn't have this format, this failed with the exception you saw.由于字符串没有这种格式,因此您看到的异常失败了。

Links链接

Oracle tutorial: Date Time explaining how to use java.time . Oracle 教程:解释如何使用java.time日期时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM