user1737894
user1737894

Reputation:

Calculate the Date

Friends I am getting two inputs from the user

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

I need to calculate the FinalDate such that

FinalDate=InitDate+numberOfDates

What I have done till now

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;

}

Upvotes: 3

Views: 165

Answers (3)

flash
flash

Reputation: 6810

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 your final date with:

Date finalDate = cal.getTime();

And you don't have to use a third party api.

Upvotes: 2

dunn less
dunn less

Reputation: 623

you could probably do something similar to this.

private DatePicker initPicker;
dp = (DatePicker) findViewById(R.id.initPicker);
final Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR,initPicker.getYear());
    c.setMonth(Calendar.MONTH,initPicker.getMonth());
    c.setDayOfMonth(Calendar.DAY_OF_MONTH,initPicker.getDayOfMonth());
long millis = c.getTimeInMillis();

For the different date picker objects. then add or subtract the millis in what ever sequence you need.

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Use Joda DateTime library. DateTime#plusDays will add days.

//Initialize your date
DateTime dateTime = DateTime(...);

dateTime.plusDays(days);

Very easy to handle date and this library will be added to Java 8.

Upvotes: 1

Related Questions