Anna
Anna

Reputation: 203

ParseException: Unparseable date: "04 December"

I have the birthday date 04 December I want to save it as 04-12 in the database, for that I do this:

val birthday = theForm.field("birthday") //String
val date = new java.text.SimpleDateFormat("dd-mm", Locale.ENGLISH).parse(birthday)

But i get the error: ParseException: Unparseable date: "04 December"

Any idea? Thanks!

Upvotes: 1

Views: 1076

Answers (2)

Epicurist
Epicurist

Reputation: 923

What about:

import java.time._

val  birthDay = MonthDay.parse("--12-04")

Upvotes: 0

cmbaxter
cmbaxter

Reputation: 35443

Your date format string is not correct. Try it as "dd MMMM". The javadocs for SimpleDateFormat are pretty comprehensive for format possibilities:

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

It seems like you want to parse it as one format and then re-format it into another. For this you can use two separate SimpleDateFormat instances, one with "dd MMMM" for parsing the 04 December format and one with "dd-MM" for re-formatting into the format you want to save to your db. The code would look like this:

val date = new SimpleDateFormat("dd MMMM", Locale.ENGLISH).parse(birthday)
val dbDateString = new SimpleDateFormat("dd-MM", Locale.ENGLISH).format(date)

Upvotes: 5

Related Questions