还有另一种方法可以在 android 日历选择器中添加天数吗

Is there another way to add days in android calendar picker

我正在尝试添加超过月份天数的天数。例如 2019 年 7 月 1 日,我加上 32 天,所以结果是 2019 年 8 月 2 日。

SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyy");
SimpleDateFormat Dateformat = new SimpleDateFormat("mm/dd/yyy");
String getDate = date_pick.getText().toString();
Date mDate;
Date result_desu;
try {
    mDate = format.parse(getDate);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(mDate);
    calendar.add(Calendar.DATE, 32);
    String formattedDate = Dateformat.format(calendar.getTime());
    date_result.setText(formattedDate);    // format output
} catch (ParseException e) {
    e.printStackTrace();
}

我一直在使用这段代码,但结果是日期只在同一个月重置,例如:2019 年 7 月 1 日;结果:2019年7月2日

试试这个:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Main{
   public static void main(String args[]){
    String oldDate = "2019-07-1";  
    System.out.println("Date before Addition: "+oldDate);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    try{
       c.setTime(sdf.parse(oldDate));
    }catch(ParseException e){
        e.printStackTrace();
     }

    c.add(Calendar.DAY_OF_MONTH, 32);  
    String newDate = sdf.format(c.getTime());  
    System.out.println("Date after Addition: "+newDate);
   }
}

输出:

Date before Addition: 2019-07-1
Date after Addition: 2019-08-02