[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Calculating 7 Days Prior for a known date?

Posted on 2002-03-14
5
Medium Priority
?
947 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 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