Solved

Add 14 Days to inputted Date

Posted on 2007-11-19
11
418 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

829 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