Solved

Add 14 Days to inputted Date

Posted on 2007-11-19
11
419 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
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!

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 37
How to convert from xls to xlsx using java 7 84
sql import cannot be resolved jsp 3 74
What browser will run Java? 7 122
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 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