Hello,

can you guys please sugest a way to have today date substracted 7 days, and have al of this 7 days into one String array?

thanks.

Use the Calendar class for performing Date related operations, esp the add() and set() methods. A sample snippet in Groovy:

import java.util.*

def c = Calendar.getInstance()
c.set(Calendar.DAY_OF_MONTH, 2)
c.set(Calendar.MONTH, 2)
// c now contains the date 2nd of March 2011
def i = 1
while(i <= 7) {
  c.add(Calendar.DAY_OF_MONTH, -1)
  println c.getTime()
  i++
}
OUTPUT:
Tue Mar 01 11:31:45 IST 2011
Mon Feb 28 11:31:45 IST 2011
Sun Feb 27 11:31:45 IST 2011
Sat Feb 26 11:31:45 IST 2011
Fri Feb 25 11:31:45 IST 2011
Thu Feb 24 11:31:45 IST 2011
Wed Feb 23 11:31:45 IST 2011

Use the SimpleDateFormat for converting the Date object to a desired representation.

Thanks ~s.o.s~
this works perfect :)

also I came up with another solution, (I was lazy a couple hours ago).
I'm leaving it here.

public static String[] SevenDaysFromNow(){
        String[] days = new String[7];
        Date today = new Date();
        long times = today.getTime();
        long oneday = (24 * 60 * 60 * 1000);
        for(int i=0; i<7; i++){
            times -= oneday;
            Date tmp = new Date(times);
            String DATE_FORMAT_NOW = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String MyDate = sdf.format(tmp);
            days[i] = MyDate;
        }
        return days;
    }

this will output dates in yyyy-mm-dd (MySQL friendly)
but your solution is awesome.

I will mark this as solved.
thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.