0

I have a "May 07, 2015" string. I want to convert it to DateTime format as 2015-05-07 pattern .
I have converted as follows:

scala> val format = new java.text.SimpleDateFormat("MMM dd, yyyy")
format: java.text.SimpleDateFormat = java.text.SimpleDateFormat@2e2b536d

scala> format.parse("May 07 2015")
res5: java.util.Date = Thu May, 07 00:00:00 IST 2015

What should be the next step to convert the above int 2015-05-07 without writing a map to convert the months to their corresponding numeric values?

Premraj
  • 72,055
  • 26
  • 237
  • 180
Goku__
  • 940
  • 1
  • 12
  • 25
  • I dont get your question. You have a date object (representing a date). You understand that you can create date formatters. What prevents you to from creating another custom formatter, and apply that to your date object? – GhostCat May 11 '15 at 12:26
  • See related question http://stackoverflow.com/questions/18823627/java-string-to-datetime and http://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library. – Mihai8 May 11 '15 at 12:27

2 Answers2

3

Because you already use SimpleDateFormat why do't you use a second DateFormat to format your date, e.g.

val df = new java.text.SimpleDateFormat("yyyy-MM-dd")
df.format(*date*)

To complete your example:

val format = new java.text.SimpleDateFormat("MMM dd yyyy")
val myDate = format.parse("May 07 2015")
val df = new java.text.SimpleDateFormat("yyyy-MM-dd")
df.format(myDate)
Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
0

You have parsed the string now you have to format it for that create a new SimpleDateFormat object with your required format and use format method to format the date.

scala> val format = new java.text.SimpleDateFormat("MMM dd yyyy")
format: java.text.SimpleDateFormat = java.text.SimpleDateFormat@2e2b536d

scala> format.parse("May 07 2015")
res0: java.util.Date = Thu May 07 00:00:00 IST 2015

scala> val format1 = new java.text.SimpleDateFormat("yyyy-MM-dd")
format1: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> var newDate=format1.format(res0)
newDate: String = 2015-05-07
singhakash
  • 7,891
  • 6
  • 31
  • 65