0

This is my code in scala

 var s:String ="22/08/2013"
 var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
 var  date:Date = simpleDateFormat.parse(s);
 println(date)

the date is not change. the format of date same as 22/08/2013 there is no change to 2013/08/22

How to change the format dd/mm/yyyy to yyyy/mm/dd in scala

Prasanth A R
  • 4,046
  • 9
  • 48
  • 76
  • 1
    You may want to have look on date formatter tutorial .. http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html – Shrey Aug 29 '13 at 07:30
  • Modern comment: I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 05 '20 at 04:48
  • Does this answer your question? [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string). This could be relevant too? [want current date and time in “dd/MM/yyyy HH:mm:ss.SS” format](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format). – Ole V.V. Jun 05 '20 at 04:56

3 Answers3

7

You need to define a SimpleDateFormat which first parses your string and get Date from it. And then convert it to whatever format.

 var s:String ="22/08/2013"
 var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
 var  date:Date = simpleDateFormat.parse(s);
 val ans = new SimpleDateFormat("yyyy/mm/dd").format(date) 
 println(ans)
Jatin
  • 31,116
  • 15
  • 98
  • 163
1

I tried this it works for me.

val s: String = "22/08/2013"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("yyyy/mm/dd")
println(df.format(date))
Vishal John
  • 4,231
  • 25
  • 41
  • i need df as util date how to do that,., – Prasanth A R Aug 29 '13 at 05:52
  • sorry, i don't know of a standard utility function which can convert between formats. will have to write custom helper functions. – Vishal John Aug 29 '13 at 06:07
  • 1
    @zzztimbo why do you think so. If you see the time of answering, we both answered in 1 min difference. Also i did not copy paste the other answer. If you observe closely, I have followed the Scala convention of using `val`s instead of `var`s – Vishal John Oct 03 '15 at 14:03
  • @user007 I apologize, I didn't notice the vals. Using vars is absurd. – zzztimbo Oct 03 '15 at 16:20
1

There's a subtle error in John Vishal's answer: mm defines 08 as minutes, not as month. Use MM instead.

Corrected code:

val s = "2013_08_14"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy_MM_dd")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("dd-MMM-yyyy")
println(df.format(date))
M.S.Visser
  • 61
  • 1
  • 6
  • Correct, even both of the other answers have that error. We shouldn’t waste our time on `SimpleDateFormat` anyway, though. It’s a notoriously troublesome class and long outdated. – Ole V.V. Jun 05 '20 at 04:54