Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a date x days back from a custom date in Scala

I have a date key of type 20170501 which is in YYYYmmdd format. How can we get a date x days back from this date in Scala?

This is what I have in program

val runDate = 20170501

Now I want a date say 30 days back from this date.

like image 579
ankitbaldua Avatar asked Dec 24 '22 17:12

ankitbaldua


1 Answers

Using Scala/JVM/Java 8...

scala> import java.time._
import java.time._

scala> import java.time.format._
import java.time.format._

scala> val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
formatter: java.time.format.DateTimeFormatter = Value(YearOfEra,4,19,EXCEEDS_PAD)Value(MonthOfYear,2)Value(DayOfMonth,2)

scala> val runDate = 20170501
runDate: Int = 20170501

scala> val runDay = LocalDate.parse(runDate.toString, formatter)
runDay: java.time.LocalDate = 2017-05-01

scala> val runDayMinus30 = runDay.minusDays(30)
runDayMinus30: java.time.LocalDate = 2017-04-01
like image 181
Steve Waldman Avatar answered Jan 15 '23 17:01

Steve Waldman