I am trying to figure out how this works. My initial date is: "2022-11-06T08:39:16.307Z"
So now I did this:
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
sdf.timeZone = TimeZone.getTimeZone("UTC")
val time: Long = sdf.parse("2022-11-06T08:39:16.307Z").time
val calendar = Calendar.getInstance()
val currentTime = calendar.timeInMillis
val diff = (currentTime - time) / 1000
Difference is negative number but it should be positive as other date is in past. Yes I hate working with dates in general but can not figure out why it works like this.
java.time.*instead ofSimpleDateFormat?Date,Calendar,SimpleDateFormatand the like are deprecated java legacy. Since around 10 years, you are supposed to use the stuff from thejava.timepackage. SoInstant,ZonedDateTime,OffsetDateTime,LocalDateTime,LocalDate,LocalTimeandDateTimeFormatter. They are much easier to use, have no bugs, are less confusing and generally just better.Duration.between(Instant.parse(text), Instant.now()). And then maybe stuff likeduration.getSeconds()or whatever you are interested in.