我已经从 SQLiteDatabase 中检索了一个日期,并通过以下方式将其格式化为我想要的格式;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
我现在想让用户选择将 steepingdate 中的任何日期增加用户可以输入的特定天数
我知道你可以用;
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 10);
例如在今天的日期上加上 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);
我遇到了错误;
坏类:类 java.util.GregorianCalendar
有什么想法吗?
最佳答案
要向 steepingdate
添加 10 天,您可以使用:
Calendar calendar = Calendar.getInstance();
calendar.setTime(steepingdate);
calendar.add(Calendar.DAY_OF_YEAR, 10);
如果提供了数字,通过用户界面,您可以使用 View.OnClickListener
,当触发 onClick 时,从 EditText
中读取值,然后使用这个值而不是 10
关于Android - 如何添加日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040269/