-2

I am working on an app and i need to get the difference between the actual date and a date inserted by the user, in days and in double.

Any idea on how to make this? I've tried some things but without success.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
YUCA
  • 1
  • 1
  • 4
    what are the "things" you tried? – tyczj Apr 16 '18 at 20:40
  • 1
    in double... what? – Phantômaxx Apr 16 '18 at 20:55
  • I think in [`Double`](https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html) – stkent Apr 16 '18 at 21:44
  • And the things you tried, in which way were they insufficient or failed? We can guide you much better when we know where you are(!) Also please give example of input and expected result since “in double” is rather unclear/ambiguous. – Ole V.V. Apr 17 '18 at 08:56
  • If the user inserts the date as a string, parse it. I suggest using `LocalDate` and `DateTimeFormatter.ofLocalizedDate()`, both are available in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). Use `ChronoUnit.DAYS.between()` for days between the dates. All assuming Java is your programming language or at least your platform. If it’s too vague, please edit your question to be more precise. – Ole V.V. Apr 17 '18 at 09:01
  • Welcome to Stack Overflow. We’re probably pretty strict with what we accept as a good question here, which is why your question is on the brink of being put on hold. Please go through [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Ole V.V. Apr 17 '18 at 09:03

3 Answers3

2

First you must decide if you want to consider the time of the day and the timezone to calculate the difference, because this can lead to different results.

Example: current date (AKA "today") is April 17th or 18th, depending on where in the world you are. Actually, depending on the time of the day, there might be 3 different "todays" in the world, at the same time. What timezone are you using to calculate the difference?

  • the user will enter a date: only day, month and year? Will it enter the hours? Are you using the user's device's timezone or some specific zone?
  • the same questions apply to the current date

Depending on the choices you make, you might get a different result.

Anyway, I'd use this lib: http://www.threeten.org/threetenbp/ or java.time classes, if available in your API level. In both API's you can use the following.

To use a date (day-month-year only) and the device's default timezone, I'd choose the LocalDate class:

// current date in device's default timezone
LocalDate now = LocalDate.now();

// some date from input values (May 10th 2018)
LocalDate dt = LocalDate.of(2018, 5, 10);

// difference in days
long diff = ChronoUnit.DAYS.between(now, dt); // 23

If you want to consider the time of the day (hours, minutes, etc), use a LocalDateTime. But in this case, ChronoUnit.DAYS considers a day has passed when the time is >= the other (ex: the difference between April 17th at 10 AM and April 18th 9:59 AM is zero days, because the time of the day didn't reach 10 AM, so it didn't complete 1 day - with LocalDate this doesn't happen because this class doesn't have time-of-the-day fields and considers only the day, month and year).

If you want to consider everything (date, time, and timezone), including Daylight Saving Time transitions, use a ZonedDateTime instead (the code is very similar, the only difference is that you can choose a timezone to work with):

// current date/time in device's default timezone
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());

// some date from input values (May 10th 2018, 10 AM in New York timezone)
ZonedDateTime dt = ZonedDateTime.of(2018, 5, 10, 10, 0, 0, 0, ZoneId.of("America/New_York"));

// difference in days
long diff = ChronoUnit.DAYS.between(now, dt); // 23

You can choose between the device's default timezone (ZoneId.systemDefault()) or a specific one (ZoneId.of("America/New_York")). You can check all the available timezones with ZoneId.getAvailableZoneIds().

Maybe it doesn't make sense to use current date in one timezone and user's date in another (I'd use the same for both), but that's up to you to decide.

Strll
  • 21
  • 1
0
Calendar c = Calendar.getInstance();
Calendar c2 //  = what you will get from the user
long diff = c.getTimeInMillis()-c2.
double days = (double) diff/(1000*60*60*24);

that is what i have in mind. I hope this helps

0

use this way

public static double getTimeDiffBetweenDate(Date startDateTime, Date finishDateTime) {
    long diffInMilliseconds = finishDateTime.getTime() - startDateTime.getTime();
    return TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds) / 60.0;
}
Ramkumar.M
  • 681
  • 6
  • 21