Solved

Add 14 Days to inputted Date

Posted on 2007-11-19
11
413 Views
Last Modified: 2010-04-21
I have a string that gets inputted then printed on the console as a date int the MM/dd/yyyy format. I would like to add another date which would be an approximate ship date. So I need to add 14 days to the order date. I'm not real sure where to start, I have found problems with the month and year changing if the days are to much for the given month. Any help would be appreciated. Thanks
String orderDate;

	

	scanner.useDelimiter(System.getProperty("line.separator"));

				

	System.out.print("Enter the date of order(e.g 11/01/2007):");

	orderDate = scanner.next();

	

	String strFormat = "MM/dd/yyyy";

	String strDate = orderDate;

	DateFormat myDateFormat = new SimpleDateFormat(strFormat);

	Date myDate = null;

	try {

	     myDate = myDateFormat.parse(strDate);

	} catch (Exception e) {

	     System.out.println("Invalid Date ");

	     e.printStackTrace();
 

	}

	

	if (myDate != null) {

	

		String DATE_FORMAT = "MMMM dd, yyyy"; 

		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

		String currentTime = sdf.format(myDate);

System.out.println("         Date Ordered:            "+currentTime);}}}

Open in new window

0
Comment
Question by:edwest25
  • 5
  • 4
  • 2
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20318151
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 14);
date = cal.getTime();
0
 

Author Comment

by:edwest25
ID: 20318168
I know this is going to sound crazy but where would I place this code? And would this give me an additional date from the one I got above? The one above displays the date ordered on the console and I also need to show a shipped date which would be the second date on the console. thanks
0
 
LVL 12

Accepted Solution

by:
keyurkarnik earned 500 total points
ID: 20318189
Here is the code you require.
Just insert it after you have parsed the date into myDate (after line 13 in your code)
import java.util.Calendar;
 
 
 
 

Calendar calendar = Calendar.getInstance();

calendar.setTime(myDate);

calendar.add(Calendar.DATE, 14);

myDate = calendar.getTime(); // this will be current + 14

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 20318201
> I know this is going to sound crazy but where would I place this code? And would this give me an additional date from the one I got above?

put it where you want to calc the new date

      Date shippedDate = null;
      if (myDate != null) {

         Calendar cal = Calendar.getInstance();
         cal.setTime(myDate);
         cal.add(Calendar.DATE, 14);
         shippedDate = cal.getTime();
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 20318203
Add the imports at the top
And this code after line 13 as I stated above
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:edwest25
ID: 20318210
Thank you that work but now I have to format it to the MM/dd/yyyy.
0
 
LVL 92

Expert Comment

by:objects
ID: 20318230
> Thank you that work but now I have to format it to the MM/dd/yyyy.

use the same code you use to format other date

                DateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
                String shippedTime = mmddyyyy.format(shippedDate);
0
 

Author Comment

by:edwest25
ID: 20318238
Thank you all very much I got it solved!!
0
 
LVL 92

Expert Comment

by:objects
ID: 20318242

actually u already have a format

String shippedTime =myDateFormat.format(shippedDate);
0
 

Author Closing Comment

by:edwest25
ID: 31410072
Thanks for breaking it down Barney style, it really helps that way
0
 
LVL 92

Expert Comment

by:objects
ID: 20318310
how is that different from what I already posted?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

743 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