I've got a list of dates in format "yyyy-MM-dd"
, I'd like to have a number of days between my today date "2017-04-15" and first date from list which is higher than mine today date.

- 49
- 1
- 2
- 6
-
2What did you try ? – Erwin Bolwidt Apr 15 '17 at 16:09
-
3please read [how to ask good question](https://stackoverflow.com/help/how-to-ask) – Omore Apr 15 '17 at 16:10
-
2Probably not even search. – Ramandeep Nanda Apr 15 '17 at 16:11
-
Please show us the relevant code. – Tim Biegeleisen Apr 15 '17 at 16:14
-
1What doesn't work in the code that you have shown? – M. Prokhorov Apr 15 '17 at 16:26
-
can you try my answer? – strash Apr 15 '17 at 16:41
-
if you have your events as Date – strash Apr 15 '17 at 16:41
-
This is one of the things that have become so much easier with the `java.time` classes introduced in Java 8 (and backported to Java 6 and 7). – Ole V.V. Apr 15 '17 at 18:36
-
Hint: Search for `java.time.LocalDate` and `ChronoUnit.DAYS`. Already asked and answered many many times over on Stack Overflow. – Basil Bourque Apr 15 '17 at 18:53
3 Answers
I am assuming that your events are not sorted by date. I am assuming that you can use Java 8. This is one of the tasks that have become so much easier with the java.time
classes introduced in Java 8 (and backported to Java 6 and 7).
Use LocalDate.now()
to get today’s date.
Iterate through your events, all the time keeping track of the closest future event date. For each event use LocalDate.parse()
to convert the event’s date to a LocalDate
. The 1-arg parse
method fits your format. Compare with today’s date and with the earliest future event date encountered so far; if between, store as the new closest date. Use isAfter()
and/or isBefore
for the comparisons.
After your loop, you will either know the date or you will know that there are no future events at all. In the former case, use ChronoUnit.DAYS.between()
to get the number of days from the current date to the event date.
Solution 1
If you are using joda library, then it will be easy, you can use Days.daysBetween
:
Date startDate = ...;
Date endDate = ...;
int nbrDays = Days.daysBetween(new LocalDate(startDate), new LocalDate(endDate)).getDays();
Solution 2
Date startDate = ...;
Date endDate = ...;
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
int day1 = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(endDate);
int day2 = cal.get(Calendar.DAY_OF_MONTH);
int nbrDays = day1 - day2;
System.out.println(nbrDays);
You have to import :
import java.util.Calendar;
import java.util.Date;
Solution 3
If your dates are in this format "yyyy-MM-dd"
so you can have two dates like this :
String date1 = "1991-07-03";
String date2 = "2017-04-15";
What you should to do, split your dates with -
:
String spl1[] = date1.split("-");
String spl2[] = date2.split("-");
Calculate the difference between the two dates :
int year1 = Integer.parseInt(spl1[0]);
int month1 = Integer.parseInt(spl1[1]);
int days1 = Integer.parseInt(spl1[2]);
int year2 = Integer.parseInt(spl2[0]);
int month2 = Integer.parseInt(spl2[1]);
int days2 = Integer.parseInt(spl2[2]);
//make some calculation and in the end you can get the diffidence, this work i will let it for you.

- 55,661
- 15
- 90
- 140
-
-
you have to download the library the joda library, you can easily find it in good – Youcef LAIDANI Apr 15 '17 at 16:23
-
I can't I've to do that without downloading any library, I can import only – stackish Apr 15 '17 at 16:24
-
@stackish then you can check my second solution, you have to import `java.util.Calendar; and java.util.Date;` library – Youcef LAIDANI Apr 15 '17 at 16:29
-
@stackish check the 3rd solution this can gives you an idea also – Youcef LAIDANI Apr 15 '17 at 16:41
-
-
yes @RobinTopper exactly i want to put it but i'm lazy haha, in fact there are many ways thank you any way :) – Youcef LAIDANI Apr 15 '17 at 17:06
-
I’ve read that JodaTime is now in maintenance mode and that you should prefer to use `java.time`. If you aren’t using Java 8 or later yet, you can get them in the [ThreeTen Backport (follow the link)](http://www.threeten.org/threetenbp/). – Ole V.V. Apr 15 '17 at 18:50
This should solve your problem.
SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<Date> dateList = new ArrayList<Date>();
try {
beforeDate = myDateFormat.parse("2016-01-13");
dateList.add(myDateFormat.parse("2016-01-10"));
dateList.add(myDateFormat.parse("2016-01-11"));
dateList.add(myDateFormat.parse("2016-01-12"));
dateList.add(myDateFormat.parse("2016-01-19"));
dateList.add(myDateFormat.parse("2016-01-20"));
dateList.add(myDateFormat.parse("2016-01-21"));
} catch (ParseException e) {
System.out.println(e.getMessage());
}
//add here
boolean check = true;
for(int i = 0; check && i < dateList.size();i++){
if(dateList.get(i).after(beforeDate)){
afterDate = dateList.get(i);
check = false;
}
}
System.out.println(beforeDate+" "+afterDate);
long days = ChronoUnit.DAYS.between(LocalDate.parse(myDateFormat.format(beforeDate)), LocalDate.parse(myDateFormat.format(afterDate)));
if(days>0){
System.out.println(days);
}else{
System.out.println(0-days);
}
if you want to sort dateList then want to get afterDate then use this code after addition of date elements in dateList
Collections.sort(dateList,new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
});
This will allow you to sort dates in ascending order..

- 741
- 1
- 8
- 14
-
for more info about that class visit here :: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html#between-java.time.temporal.Temporal-java.time.temporal.Temporal- – SmashCode Apr 15 '17 at 17:16
-
If you can use `LocalDate` and `ChronoUnit`, this is certainly a good idea. In this case, you will want to avoid the `SimpleDateFormat` and `Date` altoghether and only use the newer classes. And thanks for the link in the comment. – Ole V.V. Apr 15 '17 at 18:48
-
FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Apr 16 '17 at 04:29
-
@BasilBourque thank you for info. My hand moved with SimpleDateFormat as i got used for it sure will replace that part. Thanks for comment. – SmashCode Apr 16 '17 at 04:32