37

I am using

DateFormat dateFormat = 
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = new Date();
String fromdate = dateFormat.format(date);

to get the current date, how can I get the date 7 days back. For example, if today is 7th June 2013, how can I get 31th May 2013 in the same format as defined in date formatter?

Update

Got the solution:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

        Date date = new Date();
        String todate = dateFormat.format(date);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -7);
        Date todate1 = cal.getTime();    
        String fromdate = dateFormat.format(todate1);
halfer
  • 19,824
  • 17
  • 99
  • 186
Tanu Garg
  • 3,007
  • 4
  • 21
  • 29
  • just think of it as rolling a calendar that you would generally do on your wrist watch http://www.tutorialspoint.com/java/util/calendar_roll.htm – Prateek Jun 07 '13 at 10:36

6 Answers6

60

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.

Shravan40
  • 8,922
  • 6
  • 28
  • 48
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Can't edit it cause it's only one character, but it is missing a semicolon at the end of the first line or it won't compile. – Drubio Feb 28 '18 at 09:08
22

Or use JodaTime:

DateTime lastWeek = new DateTime().minusDays(7);
Adi B
  • 1,189
  • 13
  • 32
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 2
    new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS)); – slott Sep 15 '16 at 08:40
12

You can use this to continue using the type Date and a more legible code, if you preffer:

import org.apache.commons.lang.time.DateUtils;
...
Date yourDate = DateUtils.addDays(new Date(), *days here*);
WyllianNeo
  • 341
  • 4
  • 14
11

Java now has a pretty good built-in date library, java.time bundled with Java 8.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Foo {
    public static void main(String[] args) {

        DateTimeFormatter format =
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime then = now.minusDays(7);

        System.out.println(String.format(
            "Now:  %s\nThen: %s",
            now.format(format),
            then.format(format)
        ));
        /*
            Example output:
                Now:  2014-05-09T14:51:48Z
                Then: 2014-05-02T14:51:48Z
         */
    }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
10

Use the Calendar-API:

// get Calendar instance
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());

// substract 7 days
// If we give 7 there it will give 8 days back
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-6);

// convert to date
Date myDate = cal.getTime();

Hope this helps. Have Fun!

Matt S.
  • 13,305
  • 15
  • 73
  • 129
SimonSez
  • 7,399
  • 1
  • 30
  • 35
1

For all date related functionality, you should consider using Joda Library. Java's date api's are very poorly designed. Joda provides very nice API.

vineet
  • 336
  • 2
  • 11