-2

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.

stackish
  • 49
  • 1
  • 2
  • 6

3 Answers3

1

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

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..

SmashCode
  • 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