1

I want to calculate how many day difference between 2 timestamps, but i do not want to consider the time difference.

For example :

long time1 = 1546258765000  (Mon 31 December 2018 13:19:25)
long time2 = 1546005915367 (Fri 28 December 2018 15:05:15)

The result should be 3, 3 days left for expire... Due to time I get 2 from this method:

TimeUnit.DAYS.convert(time1 - time2 , TimeUnit.MILLISECONDS))

I just need to set the time same for both time1 and time2, and then go back to timestamp and calculate like this... but I am not sure what is the best way to do it.

xingbin
  • 27,410
  • 9
  • 53
  • 103
a.solak
  • 69
  • 2
  • 8
  • Which time zone are you in? Seems you are at UTC offset +01:00? You need to decide a time zone in order to convert your `long` values into dates. – Ole V.V. Dec 28 '18 at 14:36

4 Answers4

5

NOTE: As noted by Ole V.V: this only works for UTC. Since timestamps are always on UTC, if you are in another timezone it might return undesired results. Example:

In GMT + 1:

time1 = 1546216200000L (Mon 31 December 2018 01:30:00) (31/12 00:30 on UTC)
time2 = 1545953400000L (Fri 28 December 2018 00:30:00) (27/12 11:30 on UTC)

This will result in a 4 days difference, since that's the difference on UTC.

To compensate that, you should offset the difference so the timestamps show your current time, instead of UTC time. (If you are in GMT+1, for example, you will need to add 1 hour (3600000 ms) to each timestamp).


I believe the simplest way might be using module:

final long MILLIS_PER_DAY = 1000*60*60*24;
long time1 = 1546258765000L; // (Mon 31 December 2018 13:19:25)
long time2 = 1546005915367L; // (Fri 28 December 2018 15:05:15)

// Set both times to 0:00:00
time1 -= time1 % MILLIS_PER_DAY;
time2 -= time2 % MILLIS_PER_DAY;

And then

TimeUnit.DAYS.convert(time1 - time2 , TimeUnit.MILLISECONDS))

should give you the desired result.

dquijada
  • 1,697
  • 3
  • 14
  • 19
  • 1
    time1 % MILLIS_PER_DAY is always 0 – a.solak Dec 28 '18 at 14:21
  • No, it isn't. I just tested it just in case I mistyped something, but it works perfectly – dquijada Dec 28 '18 at 14:25
  • 1
    you are totally right, it was my mistake, thank you so much for the help – a.solak Dec 28 '18 at 14:32
  • 1
    This will give the expected result in UTC. In other time zones it sometimes will, sometimes the result will be one day too many or too few. The algorithm is very manual for my taste. I’d rather leave more work to library methods. – Ole V.V. Dec 28 '18 at 14:38
  • @OleV.V. Actually it will set both timestamps to the same time, so it doesn't really matter the timezone, it will always work fine (since we don't care about the dates, only about the difference). I usually try to avoid using library methods when it's a simple task, I don't like my programs too heavy – dquijada Dec 28 '18 at 14:53
  • 1
    I am in Europe/Copenhagen time zone. I tried your code with time1 = `1546216200000L` (Mon 31 December 2018 01:30:00) and time2 = `1545953400000L` (Fri 28 December 2018 00:30:00). Expected difference: 3 days. Difference printed: 4 days. Your code gives incorrect results outside UTC. – Ole V.V. Dec 28 '18 at 15:05
  • @OleV.V. You are right, I hadn't really considered that case, thank you for pointing it out. Edited the answer to clarify this and added a solution – dquijada Jan 02 '19 at 09:54
4

Convert millis to LocalDateTime then calculate the Duration:

LocalDateTime start = LocalDateTime 
        .ofInstant(Instant.ofEpochMilli(1546005915367L), ZoneId.systemDefault())
        .truncatedTo(ChronoUnit.DAYS);

LocalDateTime stop = LocalDateTime
        .ofInstant(Instant.ofEpochMilli(1546258765000L), ZoneId.systemDefault())
        .truncatedTo(ChronoUnit.DAYS);

Duration duration = Duration.between(start, stop);

long dayDifference = duration.toDays(); 
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Thanks a lot for the help! – a.solak Dec 28 '18 at 14:24
  • The `truncatedTo` calls in the code in this answer are crucial to getting the precise result (an alternative would have been converting to `LocalDate` since this truncates too). – Ole V.V. Dec 28 '18 at 14:40
0

Converts the given time duration in the given unit to this unit. Conversions from finer to coarser granularities truncate, so lose precision. For example, converting 999 milliseconds to seconds results in 0.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#convert(long,%20java.util.concurrent.TimeUnit)

Akceptor
  • 1,914
  • 3
  • 26
  • 31
  • 1
    I believe he knows that, at least in the sense that he knows he has to set both dates to the same time. – dquijada Dec 28 '18 at 14:20
0

Use joda-time lib:

long time1 = 1546258765000L;
long time2 = 1546005915367L;
DateTime dateTime1 = new DateTime(time1);
DateTime dateTime2 = new DateTime(time2);
int hours = Hours.hoursBetween(dateTime2, dateTime1).getHours();
int days = hours % 24 == 0 ? hours / 24 : hours / 24 + 1;
System.out.println(days);

joda-time lib has method to calculate days between two time,but the result is not your want:

Days.daysBetween(dateTime1,dateTime2).getDays()
TongChen
  • 1,414
  • 1
  • 11
  • 21