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!

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)

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?

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() );
   }
}

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

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() );
   }
}

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.