我想减少特定日期的一天。但减少了1个月。
df = new SimpleDateFormat("EEEE, dd MMMM YYYY");
Date prevDate = df.parse(dateText.getText().toString());
Calendar c = Calendar.getInstance();
c.setTime(prevDate);
c.add(Calendar.DATE, -1);
Date d=c.getTime();
dateText.setText(df.format(d));
最佳答案
java.time.LocalDate
您可以使用LocalDate
如下,
String dateText="Mon, 14 May 2018";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMMM yyyy", Locale.ENGLISH);
LocalDate localDate = LocalDate.parse(dateText, formatter);
localDate = localDate.minusDays(1);
最好避免 Date
类。但如果你必须的话,你可以转换。
Date oneDayBefore =
Date.from( // New conversion method added to the old class.
localDate
.atStartOfDay( // Determine the first moment of the day as seen in that time zone. Not always 00:00.
ZoneId.systemDefault()
) // Returns another `LocalDate` object.
.toInstant() // Returns a `Instant`.
)
; // Only if you *really* need java.util.Date object.
打印结果。
System.out.println(oneDayBefore);
String dateTextOneDayBefore = formatter.format(localDate);//Format to similar to input 'dateText'
System.out.println(dateTextOneDayBefore);
关于java - 如何在特定日期之前到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60108007/