Android - 如何添加日期

Sjh*_*son 2 android date

我从SQLiteDatabase中检索了一个Date,并通过以下内容将其格式化为我想要的格式;

String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
Run Code Online (Sandbox Code Playgroud)

我现在想让用户选择将steepingdate中的任何日期增加一定的天数,用户可以输入

我知道你可以用;

Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 10);
Run Code Online (Sandbox Code Playgroud)

例如,在今天的日期添加10天

但是你如何做到这一点,以便它使用steepingdate而不是今天的日期

谢谢

更新;

日历按我的意思工作,但我现在想将新数据保存到数据库中,完整代码如下;

String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);

Integer amountDays = Integer.parseInt(TSExtend.getText().toString());

Calendar ca = Calendar.getInstance();
ca.setTime(steepingdate);
ca.add(Calendar.DAY_OF_YEAR, amountDays);

DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
String newDate = dateFormat.format(ca);
Run Code Online (Sandbox Code Playgroud)

我收到了错误;

坏类:类java.util.GregorianCalendar

有任何想法吗?

Bla*_*elt 12

要添加10天steepingdate,您可以使用:

 Calendar calendar = Calendar.getInstance();
 calendar.setTime(steepingdate);
 calendar.add(Calendar.DAY_OF_YEAR, 10);
Run Code Online (Sandbox Code Playgroud)

它提供了数字,通过用户界面,您可以使用View.OnClickListener和onClick时触发,从中读取值EditText,并使用此值而不是10


And*_*oke 6

将日历的时间设置为您的日期,然后添加日期

Calendar cal = Calendar.getInstance();
cal.setTime(steepingdate);
cal.add(Calendar.DATE, 10);
Run Code Online (Sandbox Code Playgroud)

更新:

您无法直接格式化日历,首先从日历中获取日期,然后对其进行格式化.

String newDate = dateFormat.format(ca.getTime());
Run Code Online (Sandbox Code Playgroud)