I'm creating an application where I have calculated the start date and end date but I'm getting the output of days.
For example my start date is 2019-05-16 and I write 3 days of leave so it should give me the output for the end date 2019-05-19
Here is my source code:
public static final String DATE_FORMAT = "d/M/yyyy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Here i can use Start date as a 01/01/2018 and End Date is 01/01/2019
System.out.println ("Days: " + getDaysBetweenDates("01/02/2018","01 /01/2019"));
}
public static long getDaysBetweenDates(String start, String end) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
Date startDate, endDate;
long numberOfDays = 0;
try {
startDate = dateFormat.parse(start);
endDate = dateFormat.parse(end);
numberOfDays = getUnitBetweenDates(startDate, endDate, TimeUnit.DAYS);
} catch (ParseException e) {
e.printStackTrace();
}
return numberOfDays;
}
private static long getUnitBetweenDates(Date startDate, Date endDate, TimeUnit unit) {
long timeDiff = endDate.getTime() - startDate.getTime();
return unit.convert(timeDiff, TimeUnit.MILLISECONDS);
}
But what if I want to calculate the no of days and get the end date result.