954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding days to date

I wrote the following method to get the current date for an invoice:

public String getDate()
   {
      DateFormat dateFormat = new SimpleDateFormat("MMMMMMMMM d, yyyy");
      Date orderDate = new Date();

      return dateFormat.format(orderDate);
   }

but now I have to add two weeks (14 days) to that date (to get the expected delivery date). I've come across ways to do the same thing with a date acquired using Calendar. I tried changing my method to use Calendar instead but had issues. Is there a way to add 14 days to this date without re-doing my method? If not how would I write the method using Calendar? Thanks in advance for the help!

SMITA6076
Light Poster
37 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

to avoids conversion between SimpleDateformat and Date (or Calendar) vice versa

java.util.Calendar instance allows that

cal.add(Calendar.DATE, +14);


, java.util.Date too (but long value in milisec)

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

Is the + necessary? If so that was my mistake when I tried doing it that way. The error I got was an incompatible types error. I'll try it again tomorrow with +14 and hopefully that solves it. Why must it have a + though?

SMITA6076
Light Poster
37 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Ok I tried just adding the + and I still get the incompatible types error. Here's a little class I set up to try it out... maybe you can find what's wrong with it:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

public class DateFormatTest
{
   public String getDate()
   {
      DateFormat dateFormat = new SimpleDateFormat( "MMMMMMMMM d, yyyy" );
      Calendar calendar = Calendar.getInstance();
      return dateFormat.format( calendar.getTime() );
   }

   public String getFutureDate()
   {
      DateFormat dateFormat = new SimpleDateFormat( "MMMMMMMMM d, yyyy" );
      Calendar calendar = Calendar.getInstance();
      return calendar.add( Calendar.DATE, +14 );
   }

   public static void main( String args[] )
   {
   	  DateFormatTest date = new DateFormatTest();

   	  System.out.println( date.getDate() );
   	  System.out.println( date.getFutureDate() );
   }
}
SMITA6076
Light Poster
37 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

you had logical error only remove anything about SimpleDateFormat, isn't it

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

Check this code

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
 
public class DateFormatTest
{
   public String getDate()
   {
      DateFormat dateFormat = new SimpleDateFormat( "MMMMMMMMM d, yyyy" );
      Calendar calendar = Calendar.getInstance();
      return dateFormat.format( calendar.getTime() );
   }
 
   public String getFutureDate()
   {
      DateFormat dateFormat = new SimpleDateFormat( "MMMMMMMMM d, yyyy" );
      Calendar calendar = Calendar.getInstance();
      calendar.add( Calendar.DATE,14 );
      return dateFormat.format( calendar.getTime() );
      
   }
 
   public static void main( String args[] )
   {
   	  DateFormatTest date = new DateFormatTest();
 
   	  System.out.println( date.getDate() );
   	  System.out.println( date.getFutureDate() );
   }
}
abhishek20
Newbie Poster
17 posts since Feb 2008
Reputation Points: 8
Solved Threads: 1
 
Is the + necessary? If so that was my mistake when I tried doing it that way. The error I got was an incompatible types error. I'll try it again tomorrow with +14 and hopefully that solves it. Why must it have a + though?

No, the + is absolutely NOT necessary anywhere in Java.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

So I just need to do the calendar.add( Calendar.DATE, 14 ) before the return statement rather than in it?

SMITA6076
Light Poster
37 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
So I just need to do the calendar.add( Calendar.DATE, 14 ) before the return statement rather than in it?

It was just the wrong return type. You tried to return calendar.add(...) which isn't a String. Check http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#add(int , int)

Slimmy
Junior Poster in Training
71 posts since Oct 2010
Reputation Points: 11
Solved Threads: 11
 

Ok, I understand now. Thanks everyone for the help!

SMITA6076
Light Poster
37 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: