Question: Anyone know a simple way using Java calendar to subtract X days from a date?

I have not been able to find any function which allows me to directly subtract X days from a date in Java. Can someone point me to the right direction?

Answer: 
I believe a clean and nice way to perform subtraction or addition of any time unit (months, days, hours, minutes, seconds, ...) can be achieved using the **java.time.Instant** class.

Example for subtracting 5 days from the current time and getting the result as Date:

```
new Date(Instant.now().minus(5, ChronoUnit.DAYS).toEpochMilli());

```

Another example for subtracting 1 hour and adding 15 minutes:

```
Date.from(Instant.now().minus(Duration.ofHours(1)).plus(Duration.ofMinutes(15)));

```

If you need more accuracy, Instance measures up to nanoseconds. Methods manipulating nanosecond part:

```
minusNano()
plusNano()
getNano()

```

Also, keep in mind, that Date is not as accurate as Instant. My advice is to **stay within the Instant class**, when possible.
Someone recommended Joda Time so - I have been using this **CalendarDate** class <http://calendardate.sourceforge.net> 

It's a somewhat competing project to Joda Time, but much more basic at only 2 classes. It's very handy and worked great for what I needed since I didn't want to use a package bigger than my project. Unlike the Java counterparts, its smallest unit is the day so it is really a date (not having it down to milliseconds or something). Once you create the date, all you do to subtract is something like myDay.addDays(-5) to go back 5 days. You can use it to find the day of the week and things like that.
Another example:

```
CalendarDate someDay = new CalendarDate(2011, 10, 27);
CalendarDate someLaterDay = today.addDays(77);

```

And:

```
//print 4 previous days of the week and today
String dayLabel = "";
CalendarDate today = new CalendarDate(TimeZone.getDefault());
CalendarDateFormat cdf = new CalendarDateFormat("EEE");//day of the week like "Mon"
CalendarDate currDay = today.addDays(-4);
while(!currDay.isAfter(today)) {
    dayLabel = cdf.format(currDay);
    if (currDay.equals(today))
        dayLabel = "Today";//print "Today" instead of the weekday name
    System.out.println(dayLabel);
    currDay = currDay.addDays(1);//go to next day
}

```