I used the Calendar.getInstance().get(Calendar.DATE)
to get current date.
So the output is today (2018/05/14).
I change the device time to 2018/06/17
. So I use the Calendar
get the date. I got the 17
.
My question is I want to get 05/15
05/16
.... 06/01
... 0616
these two days. I need to know all the difference days.
So I don't know which function can do this.
Asked
Active
Viewed 68 times
-4

ManLok Wong
- 3
- 5
-
1https://developer.android.com/reference/java/time/LocalDateTime – Krzysztof Kubicki May 14 '18 at 10:10
-
1The `Calendar` class is long outdated. For your requirements the `LocalDate` class of `java.time` will serve you much better. And `java.time`, the modern Java date and time API, is also generally much nicer to work with (speaking from experience). – Ole V.V. May 14 '18 at 10:32
-
My project api level is too low. So I cannot use `LocalDate`. I don't want to update my api level – ManLok Wong May 15 '18 at 01:18
-
1@ManLokWong For earlier Android, see the *ThreeTen-Backport* and *ThreeTenABP* projects. This has been addressed hundreds of time on Stack Overflow. Search Stack Overflow before posting. – Basil Bourque May 15 '18 at 06:41
-
Certainly you can use `java.time` on low API level Android. As Basil Bourque said, add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your project. See more in [this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). I am told the dependencies are: `compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'`. – Ole V.V. May 15 '18 at 10:03
1 Answers
0
private static long daysBetween(Date one, Date two) {
long difference = (one.getTime()-two.getTime())/86400000;
return Math.abs(difference);
}
-
1Which question are you answering? Asking because I can’t see this as an answer to the question as asked. – Ole V.V. May 14 '18 at 11:50