0

Hi I have a JSON object which has a "TimeStamp" filed as below

"timestamp":1602830127

Now I want to compare this time stamp is less than 3 minutes from my current time. How can I do this in Java? And also I need to convert this into ZonedDateTime format as well

I tried something like this because then I can get it as a long number

long timestamp2 = 1602830127;
    
   
     
    for (int i = 0; i < 10; i++) {
        timestamp2 = timestamp2 + 10;
        acceptEvent(new Event(new Timestamp(timestamp2).toInstant().atZone(ZoneId.systemDefault()), UUID.randomUUID().toString()));
        //acceptEvent(new Event(ZonedDateTime.now().minusSeconds(i), UUID.randomUUID().toString()));
    }

But it always returns a wrong as 1970-01-19T18:43:50.137+05:30[Asia/Colombo] How can I achive these 2? Please help me. Thank you!

Randi
  • 639
  • 2
  • 6
  • 23
  • `new Timestamp(timestamp)` is using ms. You need to multiply seconds by 1000, `timestamp2 *= 1000;`. (and `timestamp2 = timestamp2 + 10000;` too) – Cid Oct 31 '20 at 08:54
  • 1
    I recommend you don’t use `java.sql.Timestamp`. Those classes are poorly designed and long outdated. Stick to [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 31 '20 at 09:14
  • 1
    This avoids the outdated class *and* fixes the problem you are asking about:`Instant.ofEpochSecond(timestamp2).atZone(ZoneId.systemDefault())`. In my time zone the times now start from 2020-10-16T08:35:27+02:00[Europe/Copenhagen]. – Ole V.V. Oct 31 '20 at 09:18
  • 1
    I don’t think the linked original questions answer everything you’re asking. You are also asking a lot, there’s more than one question in your question. It’s probably better to ask them as separate questions. – Ole V.V. Oct 31 '20 at 09:24

0 Answers0