2

So I have a timestamp in this format:

1990-10-31 18:43:12

I want to calculate the difference between two of these timestamps.

What I have attempted:

val t1 = new java.text.SimpleDateFormat("yyyy-mm-dd HH:mm:ss")
val t2 = new java.text.SimpleDateFormat("yyyy-mm-dd HH:mm:ss")
t1.parse(timestamp1)
t2.parse(timestamp2)

But what do I do after?

osk
  • 790
  • 2
  • 10
  • 31
  • That's now a defunct answer, now java has `java.time` – oxbow_lakes Nov 28 '17 at 10:47
  • I agree with @oxbow_lakes that you should prefer to go for a `java.time` solution. The linked question does provide a couple of `java.time` answers, just scroll down and find them. – Ole V.V. Nov 28 '17 at 13:05
  • Pure scala solution: `import scala.concurrent.duration._; (t2 milliseconds) - (t1 milliseconds) < (12 hours)` – Mitrakov Artem Aug 29 '19 at 18:29

1 Answers1

9

Firstly, Java now has java.time (JDK 1.8 and later)

import java.time._
import java.time.format._

Then (note that formatters/parsers are now stateless, it's always safe to reuse them):

val p = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") //note months are MM

val t1 = LocalDateTime.parse(timestamp1, p)
val t2 = LocalDateTime.parse(timestamp2, p)

Now you have two date/times, you need to convert to instants (actually, ZonedDateTime but it's enough for your purposes):

val i1 = t1.atZone(ZoneId.of("Europe/London"))
val i2 = t2.atZone(ZoneId.of("Europe/London"))

Now you have two instants, you can diff them:

import java.time.temporal._
val diff = i1.until(i2, ChronoUnit.SECONDS) //Or MILLIS, MICROS etc

Appendix

Here is the REPL session:

scala> :paste
// Entering paste mode (ctrl-D to finish)

val timestamp1 = "1990-10-31 18:43:12"
val timestamp2 = "1991-09-29 18:43:12"

import java.time._
import java.time.format._

val p = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") 

val t1 = LocalDateTime.parse(timestamp1, p) 
val t2 = LocalDateTime.parse(timestamp2, p)

val i1 = t1.atZone(ZoneId.of("Europe/London"))
val i2 = t2.atZone(ZoneId.of("Europe/London"))

import java.time.temporal._
val diff = i1.until(i2, ChronoUnit.SECONDS)

And here are the results:

// Exiting paste mode, now interpreting.

timestamp1: String = 1990-10-31 18:43:12
timestamp2: String = 1991-09-29 18:43:12
import java.time._
import java.time.format._
p: java.time.format.DateTimeFormatter = Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)' 'Value(HourOfDay,2)':'Value(MinuteOfHour,2)':'Value(SecondOfMinute,2)
t1: java.time.LocalDateTime = 1990-10-31T18:43:12
t2: java.time.LocalDateTime = 1991-09-29T18:43:12
i1: java.time.ZonedDateTime = 1990-10-31T18:43:12Z[Europe/London]
i2: java.time.ZonedDateTime = 1991-09-29T18:43:12+01:00[Europe/London]
import java.time.temporal._
diff: Long = 28767600
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • 1
    Good Answer but not quite right about the time zones. You can indeed calculate the span of time between two `LocalDateTime` objects. You should not arbitrarily apply any time zone. You should only apply the zone if you know for a fact that the data was intended to represent a moment in that zone but unfortunately omitted that zone info from the data itself. Tip: Always include the offset or zone info with your date-time strings, and use the standard ISO 8601 formats to do so, such as `2017-01-23T01:23:34.567Z` where the `Z` for Zulu means UTC. – Basil Bourque Nov 28 '17 at 22:05