0

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jalson1982
  • 287
  • 4
  • 14
  • 1
    Can you use `java.time.*` instead of `SimpleDateFormat`? – Sweeper Nov 06 '22 at 09:26
  • Quite new with Kotlin do you have example? Thx in advance. – Jalson1982 Nov 06 '22 at 09:28
  • 1
    Essentially, `Date`, `Calendar`, `SimpleDateFormat` and the like are deprecated java legacy. Since around 10 years, you are supposed to use the stuff from the `java.time` package. So `Instant`, `ZonedDateTime`, `OffsetDateTime`, `LocalDateTime`, `LocalDate`, `LocalTime` and `DateTimeFormatter`. They are much easier to use, have no bugs, are less confusing and generally just better. – Zabuzard Nov 06 '22 at 09:33
  • 1
    Example `Duration.between(Instant.parse(text), Instant.now())`. And then maybe stuff like `duration.getSeconds()` or whatever you are interested in. – Zabuzard Nov 06 '22 at 09:34
  • @Zabuzard even if i do this Duration.between(Instant.parse("2022-11-06T08:39:16.307Z"), Instant.now()) I get negative number. That is confusing for me. Negative seconds difference. – Jalson1982 Nov 06 '22 at 09:44
  • Check the order of your arguments. The result is negative if you put the later time on the left, i.e. if you have `Duration.between(after, before)`. That said, it is hard to help you if you do not share your full updated code. – Zabuzard Nov 06 '22 at 10:28
  • @Zabuzard what I am trying to do is to get difference in seconds between my date and now to be able to construct string like 20 minutes ago, 1 day ago etc. – Jalson1982 Nov 06 '22 at 10:37
  • If I do Duration.between(Instant.now(), Instant.parse("2022-11-06T10:38:13.857Z")) I get difference in seconds 25076. – Jalson1982 Nov 06 '22 at 10:46
  • Carefully read what I said and consult the documentation. Use methods such as `.getMinutes()`... – Zabuzard Nov 06 '22 at 11:57
  • *Quite new with Kotlin* In that case in particular you should refrain from taking classes into use that the rest of us abandoned 8 years ago or so. That is: Do not use `SimpledateFormat`, `TimeZone` nor `Calendar`. Do use `Instant` and either `ChronoUnit.SECONDS` or `Duration`. See [the Oracle tutorial](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Nov 08 '22 at 16:52
  • I cannot reproduce. I hand translated your code to Java and got 202635. That’s not negative. Are you sure 2022-11-06T08:39:16.307Z UTC was already in the past when you posted your question? – Ole V.V. Nov 08 '22 at 16:58

0 Answers0