-2

So I would like to count how many days between two datepickers. So I tried to make two array but it didnt work any Ideas?

Here my date pickers

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • [Please refer this code, this may help you.](https://stackoverflow.com/a/23323838/11158044) – Akshay Dobariya Mar 06 '19 at 10:07
  • 1
    Welcome to Stack Overflow. I don’t understand your question since I don’t see any arrays in your code. Please edit and explain. Also never post code as an image. Please paste as text and format as code. Sorry if we’re demanding. :-) – Ole V.V. Mar 06 '19 at 12:19
  • I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It will also make your task very easy: just use [`ChronoUnit.DAYS.between`](https://docs.oracle.com/javase/9/docs/api/java/time/temporal/ChronoUnit.html#between-java.time.temporal.Temporal-java.time.temporal.Temporal-). – Ole V.V. Mar 06 '19 at 12:26

4 Answers4

0

Convert your calendar string day, month, year to Date class.

More discussion here: Java string to date conversion

e.g.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d1, d2;

try {
    d1 = dateFormat.parse(year + "-" + month + "-" + day); //as per your example
    d2 = //do the same thing as above
    long days = getDifferenceDays(d1, d2);
} 
catch (ParseException e) {
    e.printStackTrace();
}


public static long getDifferenceDays(Date d1, Date d2) {
long diff = d2.getTime() - d1.getTime();
return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
Arnab Saha
  • 511
  • 6
  • 15
0

Create a method getDates()

private static ArrayList<Date> getDates(String dateString1, String dateString2)
{
    ArrayList<Date> arrayofdates = new ArrayList<Date>();
    DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");

    Date date1 = null;
    Date date2 = null;

    try {
        date1 = df1 .parse(dateString1);
        date2 = df1 .parse(dateString2);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar calender1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calender1.setTime(date1);
    calender2.setTime(date2);

    while(!calender1.after(calender2))
    {
        arrayofdates.add(calender1.getTime());
        calender1.add(Calendar.DATE, 1);
    }
    return arrayofdates;
}

then pass the parameter in this method to get array of dates As you are using DatePicker then

DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
    ArrayList<Date> mBaseDateList = getDates(df1.format(cal1.time), df1.format(cal2.time))
Rajat Mittal
  • 413
  • 5
  • 19
  • but How do I get the strings and my calendars out for my datepicker to... As you can see in the photo I uploaded. oh and ty for that – Aviv lev ari Mar 06 '19 at 10:32
  • Create two global variable for calendar . `calA`&`calB`. When you select date from datepicker just do this thing - `calA.setTime(cal1.getTime());`. This will change the time of **calA**. Do same for **calB**. When you pass the parameter to `getDates()`. **df1.format(cal1.time)** will automatically convert it to string. – Rajat Mittal Mar 06 '19 at 10:37
0
Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    Date d1, d2;
     Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    for (int i = 0; i < n; i++) {
        try {
            d2 = sdf.parse(in.next());
            d1 = sdf.parse(in.next());
            long differene = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
            System.out.println(Math.abs(differene));
        } catch (Exception e) {
        }
    }
Ahmed Abdalla
  • 2,356
  • 2
  • 14
  • 27
0
public static int getDaysBetweenDates(Date fromDate, Date toDate) {
    return Math.abs((int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24)));
}
Satish Ravani
  • 121
  • 1
  • 7