Link to home
Start Free TrialLog in
Avatar of KrAzY
KrAzY

asked on

Calculating 7 Days Prior for a known date?

Basically I need to know what the date was 7 days before the date I currently know.

Initially I allow the user to set the Date:

:::
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
    Date currentTime1 = new Date();
    String dateString = formatter.format(currentTime1);
    jTextFieldDate.setText(dateString);
:::

ex. 14-MAR-02

Then the user needs to know what the date was 7 days ago.  I need to subtract 7 days from the date they confirm and in that format I have above.

Thanks to all that can help.

Avatar of yongsing
yongsing

//Get current date
Calendar calendar = Calendar.getInstance();

// Get date seven days before current date
calendar.add(Calendar.DATE, -7);

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy");
Date currentTime1 = calendar.getTime();
String dateString = formatter.format(currentTime1);
jTextFieldDate.setText(dateString);
ASKER CERTIFIED SOLUTION
Avatar of maggari
maggari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KrAzY

ASKER

I keep getting this message:

"ClearPurchasesForm.java": Error #: 300 : class getInstance not found in class java.util.Calendar at line 91, column 39


The code I pasted works perfectly fine. Copy and paste the code I presented. Java as you know is case-sensitive, make sure you are using the right case for everything.

Check your imports. It should include java.util package.

Are you using new by any chance as in

Calendar calendar = new Calendar.getInstance();

that won't work..

I can't think of any other error. If u can paste ur code here.. i might be of some help.
Avatar of KrAzY

ASKER

Thanks... it was a minor error by me.