简体   繁体   中英

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"

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 .

When using .parse , the defined format of SimpleDateFormat should be exactly the same as that of the 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.

(Additional info translated from Chinese website: 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

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. java.time is the modern Java date and time API. YearMonth is a month in the calendar, as the class name says, a year and a month. Just what you need. And its toString method formats it into your desired format of yyyy-MM , which conforms with ISO 8601 too.

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. I recommend you avoid them. The modern API is so much nicer to work with.

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 ).
  • 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 . The toString call may be superfluous and just return the same string again. Then it parses it into a Date object, this works nicely. 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. Then you tried to parse this string into a new Date object using the format yyyy-MM . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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