[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calculating 7 Days Prior for a known date?

Posted on 2002-03-14
5
Medium Priority
?
948 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
ID: 6866902
//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 200 total points
ID: 6867186
//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
ID: 6869377
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
ID: 6869509
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
ID: 6869577
Thanks... it was a minor error by me.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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