[英]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.使用
.parse
, SimpleDateFormat
定义的格式应与 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 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 使用起来要好得多。
Date
or LocalDateTime
).Date
或LocalDateTime
)。 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.由于字符串没有这种格式,因此您看到的异常失败了。
Oracle tutorial: Date Time explaining how to use java.time
. Oracle 教程:解释如何使用
java.time
日期时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.