我实际上已经找到了最后三个日期,我想要做的是从每个日期减去5小时45分钟。如何实现它?减去日期值的时间
到目前为止,我已经做的代码是:
public static List<Date> getPastThreeDays() {
List<Date> pDates = new ArrayList<Date>();
for (int i = 1; i <= 3; i++) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -i);
Date date = cal.getTime();
String pastDate = sdf.format(date);
Date pstThreesDates;
try {
pstThreesDates = sdf.parse(pastDate);
pDates.add(pstThreesDates);
} catch (ParseException e) {
e.printStackTrace();
}
}
return pDates;
}
public static void main(String[] args) throws ParseException {
// for getting past three dates
System.out.println("-----Formatted Past Three Days-----");
List<Date> pastThreeDatesList = getPastThreeDays();
for (Date date : pastThreeDatesList) {
System.out.println("Orignal:" + date);
}
提示:“Date”只是Unix时代以来的毫秒数。你可以通过调用getTime()来获得该数字。然后可以在5小时45分钟内减去毫秒数。然后你可以调用'setTime'或者创建一个新的'Date' ... –
你已经在使用'Calendar.add()' - 缺少什么? – slim
另请注意,如果您使用Java 8的java.time包中的类,那么几乎所有有关日期的内容都会更加容易。 – slim