1

Date time is not parsing correctly in scala when i try to parse. showing a default time for all date's

used joda time as well as normal Simple Date Time

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat

val dateFormat="YYYY-MM-DD HH:mm:ss"
val tempString="2019-01-01 00:00:00"
println(DateTime.parse(tempString,DateTimeFormat.forPattern(dateFormat)).toString("YYYY-MM-DD"))
val dateFormat="YYYY-MM-DD HH:mm:ss"
val tempString2="2019-05-01 00:00:00"
println(DateTime.parse(tempString2,DateTimeFormat.forPattern(dateFormat)).toString("YYYY-MM-DD"))


scala> val dateFormat="YYYY-MM-DD HH:mm:ss"
dateFormat: String = YYYY-MM-DD HH:mm:ss

scala> val tempString="2019-01-01 00:00:00"
tempString: String = 2019-01-01 00:00:00

scala> println(DateTime.parse(tempString,DateTimeFormat.forPattern(dateFormat)).toString("YYYY-MM-DD"))
2019-01-01

scala> val dateFormat="YYYY-MM-DD HH:mm:ss"
dateFormat: String = YYYY-MM-DD HH:mm:ss

scala> val tempString2="2019-05-01 00:00:00"
tempString2: String = 2019-05-01 00:00:00

scala> println(DateTime.parse(tempString2,DateTimeFormat.forPattern(dateFormat)).toString("YYYY-MM-DD"))
2019-01-01

simple date format

import java.util.Date
import java.text.SimpleDateFormat
import java.sql.Timestamp

println(formatter.parse(tempString2))
println(formatter.parse(tempString))

scala> println(formatter.parse(tempString2))
Sun Dec 30 00:00:00 IST 2018

scala> println(formatter.parse(tempString))
Sun Dec 30 00:00:00 IST 2018

I am not able to pinpoint the issue. kindly hep me with this.

Raptor0009
  • 258
  • 4
  • 14
  • 1
    Use `yyyy-MM-dd HH:mm:ss`, because `DD` is *day-of-year*, and you want `dd` *day-of-month* (with both `DateTimeFormat` and `SimpleDateFormat`), but the discrepancy is because `YYYY` means *year-of-era* with `DateTimeFormat`, but *week-year* with `SimpleDateFormat`, and you want `yyyy` *year* (both). – Andreas Dec 25 '19 at 05:52
  • Thanks a lot now getting correct time. :) – Raptor0009 Dec 25 '19 at 05:54
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Joda-Time is better. Best are `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 25 '19 at 07:11
  • @OleV.V. You're wrong, but I thought that myself too, then I realized that *first* example is parsing date value `2019-01-01`, which result in the same date whether using `DD` (day-of-year) or `dd` (day-of-month). The problem really is the `YYYY` since Joda-Time and SimpleDateFormat disagree on what that means, which is why they give **different results** (`2019-01-01` vs `Dec 30, 2018`), **just like I said in my comment**. But you're right, the *second* example also suffers from the `DD` issue, since that ends up overriding the `MM` value, and getting January, not May. Please restore links. – Andreas Dec 25 '19 at 07:52
  • @Andreas I may not have been clear, but I am not wrong. The first example parses both of `2019-01-01` and `2019-05-01` into 2019-01-01, which is incorrect and which is fixed by changing `DD` to `dd` in all four format pattern strings. Changing `YYYY` to `yyyy` makes no difference here, that is, does not fix the problem. I think this is approximately what we both meant to say. – Ole V.V. Dec 25 '19 at 08:11

0 Answers0