get Days Between Dates - Java java.util

Java examples for java.util:Day

Description

get Days Between Dates

Demo Code


//package com.java2s;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.TreeSet;

public class Main {
    public static void main(String[] argv) throws Exception {
        Date startdate = new Date();
        Date endDate = new Date();
        System.out.println(getDaysBetweenDates(startdate, endDate));
    }/*from   w  w  w.j a va2 s . c  om*/

    public static TreeSet<Date> getDaysBetweenDates(Date startdate,
            Date endDate) {
        Calendar cal = new GregorianCalendar();

        cal.setTime(endDate);
        cal.add(Calendar.DATE, 1); //minus number would decrement the days
        Date nextEndDate = cal.getTime();


        //SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          
        TreeSet<Date> dates = new TreeSet<Date>();
        cal.setTime(startdate);

        while (cal.getTime().before(endDate)) {

            Date result = cal.getTime();

            dates.add(result);
            cal.add(Calendar.DATE, 1);
            // formatter.format(day);

        }
        return dates;
    }
}

Related Tutorials