Java 如何从当前日期减去30天

Java 如何从当前日期减去30天,java,android,Java,Android,我只需要知道如何从当前日期中减去天数。 dateFrom希望是dateTo-30天 Date dateFrom; Date dateTo; DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); dateTo = new Date(); txtDateTo.setText(dateFormat.format(dateTo)); 在给定日历中添加或减去指定的时间量 字段,基于日历的规则。例如,减去30天 从日历的当前时间开始,您可

我只需要知道如何从当前日期中减去天数。 dateFrom希望是dateTo-30天

Date dateFrom;
Date dateTo;

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateTo = new Date();
txtDateTo.setText(dateFormat.format(dateTo));
在给定日历中添加或减去指定的时间量 字段,基于日历的规则。例如,减去30天 从日历的当前时间开始,您可以通过调用:


只需减去一个月中的某一天

 Calendar c = Calendar.getInstance();
 c.add(Calendar.HOUR_OF_DAY, -30);
使用下面的代码

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, -30);
Date newDate = c.getTime();

到目前为止你做了什么?首先展示你的代码。请用一些代码片段更新问题,或者用这样的方式描述你到目前为止使用了哪个类来获取日期,以便贡献者能够帮助你
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, -30);
Date newDate = c.getTime();