0

I want to convert the date from dd-mm-yy to yy-mm-dd in scala, but I am having issues to make it happen

val format = new java.text.SimpleDateFormat("dd-mm-yyyy")
format.format(new java.util.Date())
output:
res0: String = 06-07-2018

but if I want to change the format to yy-mm-dd:

val format = new java.text.SimpleDateFormat("yy-mm-dd")
format.format(new java.util.Date())
output:
res2: String = 18-41-06

what did I do wrong? why the mm comes out as 41?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Misha
  • 133
  • 5
  • 15
  • 1
    It should be `dd-MM-yyyy` and `yy-MM-dd` – Xavier Guihot Jul 06 '18 at 19:02
  • 1
    awesome! thank you @ Xavier Guihot – Misha Jul 06 '18 at 19:04
  • Take a look this answer https://stackoverflow.com/questions/5377790/date-conversion May help. Greetings – JSanoja Jul 06 '18 at 19:07
  • You should really be using `java.time` package classes instead – OneCricketeer Jul 06 '18 at 22:02
  • There are many other questions about this issue. This one may match still better: [How can I format a String, which is a date, to a new date format in Java?](https://stackoverflow.com/questions/50937936/how-can-i-format-a-string-which-is-a-date-to-a-new-date-format-in-java) – Ole V.V. Jul 07 '18 at 08:14
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 07 '18 at 08:15

2 Answers2

2

The correct format is

val format = new java.text.SimpleDateFormat("yyyy-MM-dd")
Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
1

what did I do wrong?

IMHO three things:

  1. You used the long outdated Date class and the equally outdated and notoriously troublesome SimpleDateFormat. Instead use java.time, the modern Java date and time API.
  2. You tried to roll your own date format. For most purposes your users will be happier with Java’s built-in date formats, and they save you the error-prone writing of your own format pattern string.
  3. As others have already said, you used lowercase mm in the format pattern string. This is for minute of hour (format pattern letters are case sensitive). Apparently by coincidence you ran your first snippet at 7 minutes past the hour so it happened to agree with the current month (July).

Allow me first to demonstrate that java.time is a bit more helpful if you insist on writing your own format pattern and happen to use the wrong case (which happens for most of us from time to time). Sorry I cannot write Scala, you will have to do with Java.

    DateTimeFormatter dateFormatterWithWrongCase 
            = DateTimeFormatter.ofPattern("dd-mm-yyyy");
    LocalDate.now(ZoneId.of("America/Chicago")).format(dateFormatterWithWrongCase);

This throws the following:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour

It tells us we were trying to do something with the minute of hour, which we certainly didn’t intend. While it may not make the cause of the error very obvious at a glance, I prefer this by far over just getting an incorrect result. And the message is rather precise when you think about it since a LocalDate is a date without time of day and therefore doesn’t have minute of hour.

For getting a nice short date format for your users I suggest:

    DateTimeFormatter dateFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.SHORT)
            .withLocale(Locale.CANADA_FRENCH);
    System.out.println(LocalDate.now(ZoneId.of("America/Chicago")).format(dateFormatter));

Today this printed:

18-07-07

One advantage (and I do mean an advantage) is that the output may be different in a different locale. If I use Locale.US, I get (M/d/yy):

7/7/18

To use the default locale of your JVM either specify Locale.getDefault() or just leave out the withLocale call on the formatter.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161