Solved

Calculating 7 Days Prior for a known date?

Posted on 2002-03-14
5
942 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 50 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to debug htl and js pages 8 54
java example issue 5 43
wild fly 8 startup error 2 40
Setup GlassFish 4 35
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

749 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