Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add 14 Days to inputted Date

Posted on 2007-11-19
11
Medium Priority
?
423 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
[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
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

719 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