07 March, 2013

How to find days difference between two given dates ?

Find number of days between two given dates:-

public class FindDays {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str1="20/01/2013";
        String str2="28/03/2013";       
        Calendar cal=Calendar.getInstance();
        cal.set(Integer.parseInt(str1.substring(6)),Integer.parseInt(str1.substring(3,5))-1,Integer.parseInt(str1.substring(0,2)));
        long xTime=cal.getTimeInMillis();
        cal.set(Integer.parseInt(str2.substring(6)),Integer.parseInt(str2.substring(3,5))-1,Integer.parseInt(str2.substring(0,2)));
        long xTime2=cal.getTimeInMillis();
        System.out.println("Final Days...."+((xTime2-xTime)/(1000*60*60*24)));

    }

}


//Out put- Final Days.... 8

No comments:

Post a Comment