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: 
```
int x = -1;
Calendar cal = ...;
cal.add(Calendar.DATE, x);

```

See [`java.util.Calendar#add(int,int)`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html#add(int,int))
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.