MotaF
MotaF

Reputation: 625

java.text.ParseException: Unparseable date: "06-DEC-11"

I am following code from,Pascal Bugnion book Scala for Data Science. First class to represent transaction

case class Transaction(
id:Option[Int], // unique identifier
candidate:String, // candidate receiving the donation
contributor:String, // name of the contributor
contributorState:String, // contributor state
contributorOccupation:Option[String], // contributor job
amount:Long, // amount in cents
date:Date // date of the donation
)

defined class Transaction

Then I have loaded dat with help of FEData singleton object

scala> val ohioData = FECData.loadOhio
ohioData: FECData = FECData@7e83a375

FECData object has attribute transactions

scala> val ohioTransactions = ohioData.transactions
ohioTransactions: Iterator[Transaction] = non-empty iterator

When I try to print first 5 transactions

scala> ohioTransactions.take(5).foreach(println)
java.text.ParseException: Unparseable date: "06-DEC-11"
  at java.text.DateFormat.parse(DateFormat.java:366)
  at FECData$$anonfun$1.apply(FECData.scala:26)
  at FECData$$anonfun$1.apply(FECData.scala:16)
  at scala.collection.Iterator$$anon$11.next(Iterator.scala:370)

Let's take a look at the first 5 lines of the csv file candidate_id,candidate,contributor_name,contributor_state,contributor_occupation,amount,date

P80000748,"Paul, Ron","BROWN, TODD W MR.",OH,ENGINEER,50.0,06-DEC-11
P80000748,"Paul, Ron","DIEHL, MARGO SONJA",OH,RETIRED,25.0,06-DEC-11
P80000748,"Paul, Ron","KIRCHMEYER, BENJAMIN",OH,COMPUTER PROGRAMMER,201.2,06-DEC-11
P80003338,"Obama, Barack","KEYES, STEPHEN",OH,HR EXECUTIVE / ATTORNEY,100.0,30-SEP-11
P80003338,"Obama, Barack","MURPHY, MIKE W",OH,MANAGER,50.0,26-SEP-11

Why?

Upvotes: 0

Views: 547

Answers (1)

Federico Pellegatta
Federico Pellegatta

Reputation: 4017

Ok, the problem is that in the FECData is defined a dateParser as new SimpleDateFormat("DD-MMM-YY").

According to https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String), it constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale.

The problem is that your default locale (of your JVM) is not Locale.ENGLISH and so the DEC part of "06-DEC-11" is not parsed correctly.

You just need to patch the FECData: replace private val dateParser = new SimpleDateFormat("DD-MMM-YY") with private val dateParser = new SimpleDateFormat("DD-MMM-YY", java.util.Locale.ENGLISH).

Ref. for Localehttps://docs.oracle.com/javase/7/docs/api/java/util/Locale.html

Upvotes: 3

Related Questions