Solved

Calculating 7 Days Prior for a known date?

Posted on 2002-03-14
5
944 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

717 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