-1

I have a scenario where epoch time is in string . I need to convert this to number of days ago from current time .

Eg:

String epoch = "1600852773514";
Date expiry = new Date(Long.parseLong(epoch));

Expiry gives me Wed Sep 23 14:49:33 IST 2020 .But I want to get number of days from today to the time 'epoch' . Like epoch is 240 days ago from today.

If expiry > 30 days
pass
else
fail  
User01
  • 27
  • 6
  • 2
    What have you tried? Where is it failing? Post your code – Erwin Bolwidt May 21 '21 at 11:39
  • 1
    I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `Instant`, `LocalDate` and/or other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 21 '21 at 13:13
  • 1
    If the expiry is Wed Sep 23 14:49:33 IST 2020 and current time is Sep 24 13:02, do you want to count one day already (since the dates are different), or do you want to count 1 day only after 14:49:33 today? – Ole V.V. May 21 '21 at 13:19
  • As a possibly easier alternative add 30 days to the expiration date and time (use for example `ZonedDateTime.plusDays()`) and compare the result to the current moment. – Ole V.V. May 21 '21 at 13:23

3 Answers3

1

You can do something like this:

Date expiry = /* ... */;
Date now = new Date();

long days = (now.getTime() - expiry.getTime()) / 86_400_000;

if (days > 30) /* ... */

So we take the difference of the time in milliseconds:

long diff = (now.getTime() - expiry.getTime());

If we divide by 86.000.000 (this is how many milliseconds a day has), we get the number of past days.

Daniel
  • 1,426
  • 1
  • 11
  • 24
1

As an alternative, you could use java.time (since Java 8) and count the days between two dates:

public static void main(String[] args) {
    // example String
    String epoch = "1618991673000";
    // parse it to a long
    long epochMillis = Long.parseLong(epoch);
    // then create an Instant from that long value
    Instant instant = Instant.ofEpochMilli(epochMillis);
    // and convert it to an OffsetDateTime at UTC (+00:00)
    OffsetDateTime odt = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
    // get today's date (only, no time of day considered)
    LocalDate today = LocalDate.now();
    // and extract the date of the OffsetDateTime
    LocalDate then = odt.toLocalDate();
    // count the days between the two dates
    long daysGone = ChronoUnit.DAYS.between(then, today);
    // and print the result...
    System.out.println("Days between " + then
                        + " and today (" + today + "): " + daysGone);
}

This outputs (today, 21st of May 2021):

Days between 2021-04-21 and today (2021-05-21): 30

Afterwards, you can easily check if the amount of days is greater or less than allowed in your scenario.

deHaar
  • 17,687
  • 10
  • 38
  • 51
1

Converting to Instance's and using Duration class:

String  epoch = "1600852773514";
Instant start = Instant.ofEpochMilli(Long.parseLong(epoch));
Instant now   = Instant.now();
Long    diff  = Duration.between(start,now).toDays();

System.out.println(diff);
Sergey Afinogenov
  • 2,137
  • 4
  • 13