You can use Calendar class :

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
System.out.println("Date = "+ cal.getTime());

But as @Sean Patrick Floyd mentioned , Joda-time is the best Java library for Date.

Answer from AllTooSir on Stack Overflow
🌐
Prospera Soft
prosperasoft.com › blog › data-insights › jasperreports › java-date-seven-days-ago
Get Date for 7 Days Ago in Java | Simple Java Date Calculation Examples
By using 'calendar.add(Calendar.DAY_OF_YEAR, -7)', we effectively subtract seven days from the current date, allowing us to retrieve the required Date object. This simple but powerful approach enables developers to obtain and work with historical data effortlessly.
Top answer
1 of 11
61

From exactly now:

long DAY_IN_MS = 1000 * 60 * 60 * 24;
new Date(System.currentTimeMillis() - (7 * DAY_IN_MS))

From arbitrary Date date:

new Date(date.getTime() - (7 * DAY_IN_MS))

Edit: As pointed out in the other answers, does not account for daylight savings time, if that's a factor.

Just to clarify that limitation I was talking about:

For people affected by daylight savings time, if by 7 days earlier, you mean that if right now is 12pm noon on 14 Mar 2010, you want the calculation of 7 days earlier to result in 12pm on 7 Mar 2010, then be careful.

This solution finds the date/time exactly 24 hours * 7 days= 168 hours earlier.

However, some people are surprised when this solution finds that, for example, (14 Mar 2010 1:00pm) - 7 * DAY_IN_MS may return a result in(7 Mar 2010 12:00pm) where the wall-clock time in your timezone isn't the same between the 2 date/times (1pm vs 12pm). This is due to daylight savings time starting or ending that night and the "wall-clock time" losing or gaining an hour.

If DST isn't a factor for you or if you really do want (168 hours) exactly (regardless of the shift in wall-clock time), then this solution works fine.

Otherwise, you may need to compensate for when your 7 days earlier doesn't really mean exactly 168 hours (due to DST starting or ending within that timeframe).

2 of 11
34

Use Calendar's facility to create new Date objects using getTime():

import java.util.GregorianCalendar;
import java.util.Date;

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, -7);
Date sevenDaysAgo = cal.getTime();
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-22.php
Java - Get the dates 10 days before and after today
Java Date, Time and Calendar exercises and solution: Write a Java program to get the dates 10 days before and after today.
🌐
How to do in Java
howtodoinjava.com › home › java date time › add or subtract days, months & years to date in java
Add or Subtract Days, Months & Years to Date in Java
February 23, 2022 - Java examples to add or subtract days, months or years from java 7 Date and Java 8 LocalDate, LocalDateTime classes.
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-add-days-to-date
Java – Add days to Date
The reason I have mentioned this in a separate heading is because people ask this a lot. We have already seen how to add days to a date in Java in the above programs. To increment the date or current date by one, you just need to add 1 day to the date.
Find elsewhere
🌐
Attacomsian
attacomsian.com › blog › java-add-days-to-date
How to add days to a date in Java
October 14, 2022 - The following example shows how you can add days, years, and months to an instance of LocalDate: // increment days by 7 LocalDate date = LocalDate.now(); System.out.println("Current Date: " + date); date = date.plusDays(7); System.out.println("Date ...
🌐
Stack Overflow
stackoverflow.com › questions › 40034015 › how-to-get-the-date-from-7-days-from-now
java - How to get the date from 7 days from now - Stack Overflow
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); Calendar cal = Calendar.getInstance(); cal.setTime( dateFormat.parse( inputString ) ); cal.add( Calendar.DATE, 7 ); Share ·
🌐
Mkyong
mkyong.com › home › java › java – how to add days to current date
Java - How to add days to current date - Mkyong.com
November 15, 2016 - package com.mkyong.time; import ... public class LocalDateTimeExample { private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; private static final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); private ...
🌐
Java2Blog
java2blog.com › home › core java › java date › how to add days to date in java
How to Add Days to Date in Java - Java2Blog
November 26, 2022 - You can use add() method of Calendar class to add days to date, but this is not recommended in case you are using java 8 or later version. ... We can use Calendar's setTime() method to set current date and use add() method to add days to LocalDate. ... Current date: Tue Dec 22 13:08:11 IST ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Date.html
Date (Java Platform SE 7 )
Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases.
🌐
W3Docs
w3docs.com › java
How to subtract X day from a Date object in Java?
Here's an example of how you can subtract X days from a Date object using the Calendar class: import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] args) { // create a Calendar object and set it to the current date and time Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); // subtract 10 days from the calendar calendar.add(Calendar.DAY_OF_MONTH, -10); // get the modified Date object Date date = calendar.getTime(); // print the date System.out.println(date); } }
Top answer
1 of 8
132

Parse the date:

Date myDate = dateFormat.parse(dateString);

And then either figure out how many milliseconds you need to subtract:

Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000

Or use the API provided by the java.util.Calendar class:

Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();

Then, if you need to, convert it back to a String:

String date = dateFormat.format(newDate);
2 of 8
27

I have created my own function that may helpful to get Next/Previous date from

Current Date:

/**
 * Pass your date format and no of days for minus from current 
 * If you want to get previous date then pass days with minus sign
 * else you can pass as it is for next date
 * @param dateFormat
 * @param days
 * @return Calculated Date
 */
public static String getCalculatedDate(String dateFormat, int days) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat s = new SimpleDateFormat(dateFormat);
    cal.add(Calendar.DAY_OF_YEAR, days);
    return s.format(new Date(cal.getTimeInMillis()));
}

Example:

getCalculatedDate("dd-MM-yyyy", -10); // It will gives you date before 10 days from current date

getCalculatedDate("dd-MM-yyyy", 10);  // It will gives you date after 10 days from current date

and if you want to get Calculated Date with passing Your own date:

public static String getCalculatedDate(String date, String dateFormat, int days) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat s = new SimpleDateFormat(dateFormat);
    cal.add(Calendar.DAY_OF_YEAR, days);
    try {
        return s.format(new Date(s.parse(date).getTime()));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        Log.e("TAG", "Error in Parsing Date : " + e.getMessage());
    }
    return null;
}

Example with Passing own date:

getCalculatedDate("01-01-2015", "dd-MM-yyyy", -10); // It will gives you date before 10 days from given date

getCalculatedDate("01-01-2015", "dd-MM-yyyy", 10);  // It will gives you date after 10 days from given date
🌐
100daysofcode
100daysofcode.io › learn › java › date
Date | Java Roadmap | Java Tutorials | 100 Days of Java
Initialise a date variable with the date 7 days before the current day · Compare 2 dates with before, after and compareTo methods · Print the current date in the following time zones: GMT, CST, IST · <Prev|Next> Learning Resources |View Complete Plan · Sponsor Us|Community|Blog|Youtube...
Top answer
1 of 10
402

Java 8 and later

With Java 8's date time API change, Use LocalDate

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

Similarly you can have

LocalDate date = someLocalDateInstance.minusDays(300);

Refer to https://stackoverflow.com/a/23885950/260990 for translation between java.util.Date <--> java.time.LocalDateTime

Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());

Java 7 and earlier

Use Calendar's add() method

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

@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);
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-add-days-to-current-date-using-java-calendar
Java Program to add days to current date using Java Calendar
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime()); // Incrementing Days by 2 calendar.add(Calendar.DATE, 2); System.out.println("Updated Date = " + calendar.getTime()); } } Current Date = Thu Nov 22 16:04:55 UTC 2018 Updated Date = Sat Nov 24 16:04:55 UTC 2018 ·