36

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,471
  • 17
  • 87
  • 173
Tanu Garg
  • 2,767
  • 4
  • 19
  • 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

59

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
  • 7,746
  • 5
  • 27
  • 45
AllTooSir
  • 47,910
  • 16
  • 124
  • 159
  • 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,148
  • 13
  • 32
Sean Patrick Floyd
  • 284,665
  • 62
  • 456
  • 576
  • 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
  • 311
  • 3
  • 13
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.
  • 12,849
  • 14
  • 74
  • 128
SimonSez
  • 6,959
  • 1
  • 29
  • 35
10

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
  • 29,484
  • 8
  • 71
  • 131
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
  • 340
  • 2
  • 11