Date comparison

I am new to scala, I have to copy data from elastic index to hive. Elastic column is of DATE type and format is ‘yyyy-mm-dd’ and my input date is in string format ‘yyyymmdd’.
I have to pull data which will match with elastic column date, can someone help on these

Check the java.time package: java.time (Java Platform SE 8 )


Probably the least-to-learn (but not most efficient) is to take advantage of Scala Strings working like lists:

val elastic = "1982-03-29"
val year = elastic.take(4)
val month = elastic.drop(5).take(2)
val day = elastic.drop(8).take(2)
val hive = s"$year$month$day"

How much do you trust the date format from elastic?

Below is elastic date format.
“type”: “date”
“format” : “yyyy-mm-dd”.
If I understood correctly you are first extracting year\month\date and again concatenating it.

That’s correct.

Oh - I was right in my earlier attempt. I’ve edited it again.

That example is going to be making and then GC-ing a lot of Strings. It’s not a big deal unless you’re after tens-of-millions of dates or more on modern hardware.

My big pushback in a code review would be how this fails in an ugly way with a String like “InvalidDate” . “Invaidate” helps no one.

or just hive = elastic.replace(“-”,“”)?

1 Like