
Calculating Dates from Two Pickers in Android
Hi Folks,In this blog i am going to explain the simple concept of calculating difference between the two days in multiple date picker android. Let’s take a quick look…
Source Code:
//Simple date format
SimpleDateFormatmyFormat = new SimpleDateFormat( “dd-MM-yyyy”);
long diff = 0;
//Getting firstdate of calender
String sInputDate= etInputdate.getText().toString();
//Getting second date of calender
String sOutputDate= etOutputdate.getText().toString();
try {
Date date1 = myFormat.parse(sInputDate);
Calendar c1 = Calendar.getInstance();
//Here we are setting date1
c1.setTime(date1);
Date date2 = myFormat.parse(sOutputDate);
Calendar c2 = Calendar.getInstance();
//Here we are setting date2
c2.setTime(date2);
long ms1 = c1.getTimeInMillis();
long ms2 = c2.getTimeInMillis();
//Getting difference
diff = ms2 – ms1;
} catch (ParseException e) {
e.printStackTrace();
}
intdiffInDays = (int) (diff / (24 * 60 * 60 * 1000) + 1);
//In diffInDays you will get the output
Log.e(“Difference”,” “+ diffInDays );
Thats it folks….