0

I have an initial date as a String which I need to convert to date with a specific format. I tried to defined date format in a string and parse it, then I formatted it to the desired format. The problem is that I need a date as a final result. Here is the code I used:

  def parseDateToOriginal(date: String): String = {
    val initialDateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy")
    val finalDateFormat = new SimpleDateFormat("yyyy-mm-dd")

    val result = finalDateFormat.format(initialDateFormat.parse(date))
    result
  }

So I need Date as the return type for this method. I tried to parse the result string to get a proper date but for some reason, the result defaults back to the original date format. How can I fix this problem?

Here is how I tried to parse it again:

val parsedDate = new SimpleDateFormat("yyyy-mm-dd").parse(parseDateToOriginal(date))

The result is of the pattern "EEE MMM dd hh:mm:ss zzz yyyy"

Cassie
  • 2,941
  • 8
  • 44
  • 92
  • 1
    Possible duplicate of [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format) – Ole V.V. Jun 29 '19 at 08:09
  • 1
    You are asking the impossible. A `Date` cannot have a format. Also 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 `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And also using `zzz` for parsing is inherently unsafe since time zone abbreviations are ambiguous (I think more often than not). – Ole V.V. Jun 29 '19 at 08:10

2 Answers2

4

First, SimpleDate is old and outdated. The current java.time library is recommended.

Next, if you need to return a Date then parse the input and return the Date. You need to format a Date only when you present it, i.e. change it to a String.

import java.time.LocalDate
import java.time.format.DateTimeFormatter

def parseToDate(date: String): LocalDate = 
  LocalDate.parse(date
                 ,DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss zzz yyyy"))
jwvh
  • 50,871
  • 7
  • 38
  • 64
2

Try

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

def parseDateToOriginal(date: String): String = {
  LocalDateTime
    .parse(date, DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy"))
    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
}

which outputs

parseDateToOriginal("Thu Jun 18 20:56:02 EDT 2009") // res2: String = 2009-06-18

Note you have a bug in the format of finalDateFormat

val finalDateFormat = new SimpleDateFormat("yyyy-mm-dd")

You are using lowercase mm in the month positions, but should be upper case MM. Lowercase mm represents minutes, so it would erroneously result in res2: String = 2009-56-18 as outputs.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • 1
    The `mm` bug is nicely spotted. The questioner additionally erroneously has got lowercase `hh` in the other format pattern. You have corrected it in your code. – Ole V.V. Jun 29 '19 at 08:12