如何减去X天从Java中的Date对象?(How to subtract X day from a

2019-06-17 16:48发布

我想要做的事,如:

Date date = new Date(); // current date
date = date - 300; // substract 300 days from current date and I want to use this "date"

怎么做?

Answer 1:

Java的8和更高版本

使用Java 8的日期时间API的变化,使用LocalDate

LocalDate date = LocalDate.now().minusDays(300);

Java 7的和更早

使用Calendaradd()方法

Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -30);
Date dateBefore30Days = cal.getTime();


Answer 2:

@JigarJoshi it's the good answer, and of course also @Tim recommendation to use .joda-time.

I only want to add more possibilities to subtract days from a java.util.Date.

Apache-commons

One possibility is to use apache-commons-lang. You can do it using DateUtils as follows:

Date dateBefore30Days = DateUtils.addDays(new Date(),-30);

Of course add the commons-lang dependency to do only date subtract it's probably not a good options, however if you're already using commons-lang it's a good choice. There is also convenient methods to addYears,addMonths,addWeeks and so on, take a look at the api here.

Java 8

Another possibility is to take advantage of new LocalDate from Java 8 using minusDays(long days) method:

LocalDate dateBefore30Days = LocalDate.now(ZoneId.of("Europe/Paris")).minusDays(30);


Answer 3:

我真的建议你使用的DateTime库中找到的位置: https://www.joda.org/joda-time/

这是我的第一个依赖我添加到几乎每一个Java项目我建立的一个。

如果您有需要的日期或日历的一些遗留代码,您可以从DateTime是否将其转换回来。

所以减去300天是那么容易,因为:

Date date = new Date(); // Or where ever you get it from
Date daysAgo = new DateTime(date).minusDays(300).toDate();


Answer 4:

Java的8时API:

Instant now = Instant.now(); //current date
Instant before = now.minus(Duration.ofDays(300));
Date dateBefore = Date.from(before);


Answer 5:

与Java 8,现在真的很简单:

 LocalDate date = LocalDate.now().minusDays(300);

一个伟大的指导,以新的API,可以发现在这里 。



Answer 6:

正如你可以看到这里有很多的操作,你可以做的。 下面的例子显示,你可以做什么!

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();

//Add one day to current date.
cal.add(Calendar.DATE, 1);
System.out.println(dateFormat.format(cal.getTime()));

//Substract one day to current date.
cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(dateFormat.format(cal.getTime()));

/* Can be Calendar.DATE or
*  Calendar.MONTH, Calendar.YEAR, Calendar.HOUR, Calendar.SECOND
*/


Answer 7:

我创建了一个功能,使工作更容易。

  • 对于dateString后7天: dateCalculate(dateString,"yyyy-MM-dd",7);

  • 为了得到7天高达dateString: dateCalculate(dateString,"yyyy-MM-dd",-7);


public static String dateCalculate(String dateString, String dateFormat, int days) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat s = new SimpleDateFormat(dateFormat);
    try {
        cal.setTime(s.parse(dateString));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    cal.add(Calendar.DATE, days);
    return s.format(cal.getTime());
}


Answer 8:

您可以轻松地与SimpleDateFormat的日历减

 public static String subtractDate(String time,int subtractDay) throws ParseException {


        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH);
        cal.setTime(sdf.parse(time));
        cal.add(Calendar.DATE,-subtractDay);
        String wantedDate = sdf.format(cal.getTime());

        Log.d("tag",wantedDate);
        return wantedDate;

    }


Answer 9:

c1.set(2017, 12 , 01); //Ex: 1999 jan 20    //System.out.println("Date is : " + sdf.format(c1.getTime()));
  c1.add(Calendar.MONTH, -2); // substract 1 month
  System.out.println
  ("Date minus 1 month : "
      + sdf.format(c1.getTime()));


文章来源: How to subtract X day from a Date object in Java?