3

朋友们,我从用户那里得到了两个输入

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

我需要计算 FinalDate 这样

FinalDate=InitDate+numberOfDates

到目前为止我所做的

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;

}
4

3 回答 3

2

尝试这个:

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

您可以通过以下方式获得最终日期:

Date finalDate = cal.getTime();

而且您不必使用第三方 api。

于 2012-10-12T06:17:25.240 回答
1

使用Joda DateTime 库DateTime#plusDays将增加天数。

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

dateTime.plusDays(days);

非常容易处理日期,这个库将被添加到 Java 8 中。

于 2012-10-12T06:19:31.387 回答
0

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.

于 2012-10-12T06:27:33.250 回答