?
Solved

Add 14 Days to inputted Date

Posted on 2007-11-19
11
Medium Priority
?
427 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 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
Industry Leaders: 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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 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