Java – Calculate the date

Calculate the date… here is a solution to the problem.

Calculate the date

Friends, I got two inputs from the user

1.InitDate from DatePicker
2.Difference in between two dates (numberOfDates)

I need to calculate FinalDate like this

FinalDate=InitDate+numberOfDates

What I’ve done so far

private void CalcLastDate(int days) 
        {

long millis=days*24*60*60;

Calendar c = Calendar.getInstance();

c.set(settingsFromDate.getYear(), settingsFromDate.getMonth(), settingsFromDate.getDayOfMonth());
            long initDate = c.getTimeInMillis();

long longFinalDate=initDate+millis;

}

Solution

Try this :

Calendar cal = Calendar.getInstance();
cal.setTime(initDate); initDate must be of java.util.Date
cal.add(Calendar.DAY_OF_MONTH, numberOfDates);

You can get the final date by:

Date finalDate = cal.getTime();

And you don’t have to use third-party APIs.

Related Problems and Solutions