Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set yesterday's date using Scala?

I am creating a date in Scala.

  val dafo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")
  val tz = TimeZone.getTimeZone("UTC")
  dafo.setTimeZone(tz)
  val endTime = dafo.format(new Date())

How can I set the yesterday's date instead of today's date?

like image 833
Markus Avatar asked Dec 07 '22 15:12

Markus


2 Answers

Here is how you get yesterday's date/time and format it using java.time:

import java.time.{ZonedDateTime, ZoneId}
import java.time.format.DateTimeFormatter


val yesterday = ZonedDateTime.now(ZoneId.of("UTC")).minusDays(1)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")
val result = formatter format yesterday

println(result)
like image 132
Andrey Tyukin Avatar answered Dec 28 '22 23:12

Andrey Tyukin


You can use Calendar:

val dafo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")
val tz = TimeZone.getTimeZone("UTC")
dafo.setTimeZone(tz)

val calendar = Calendar.getInstance()
calendar.add(Calendar.DATE, -1)
dafo.format(calendar.getTime)
like image 26
vsylvestre Avatar answered Dec 28 '22 23:12

vsylvestre