Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date difference in scala [duplicate]

Tags:

date

scala

I need a function to find the difference(in number of days)between two dates in "yyyy-mm-dd" format.

val basedate = "1970-01-01"
val currdate = "2015-02-25"
val diff = currdate - basedate

Please suggest.

like image 324
Kundan Kumar Avatar asked Oct 21 '25 14:10

Kundan Kumar


1 Answers

i think You are searching something like this

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

val startDate = "1970-01-01" 
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val oldDate = LocalDate.parse(startDate, formatter)
val currentDate = "2015-02-25"
val newDate = LocalDate.parse(currentDate, formatter)
println(newDate.toEpochDay() - oldDate.toEpochDay())
like image 84
Govind Singh Avatar answered Oct 23 '25 04:10

Govind Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!