Solved

Calculating 7 Days Prior for a known date?

Posted on 2002-03-14
5
938 Views
Last Modified: 2008-03-17
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.

0
Comment
Question by:KrAzY
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:yongsing
Comment Utility
//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);
0
 

Accepted Solution

by:
maggari earned 50 total points
Comment Utility
//Set date to whatever date you want to set.
//Here I am taking the current time as date
Date date = new Date();

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//add -7 to current time you know.
calendar.add(calendar.DATE, -7);

//Get the new time which is set to 7 days prior.
Date pdate = calendar.getTime();
0
 
LVL 11

Author Comment

by:KrAzY
Comment Utility
I keep getting this message:

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


0
 

Expert Comment

by:maggari
Comment Utility
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.
0
 
LVL 11

Author Comment

by:KrAzY
Comment Utility
Thanks... it was a minor error by me.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now