0

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!

5
Contributors
9
Replies
12
Views
5 Years
Discussion Span
Last Post by SMITA6076
Featured Replies
  • Check this code [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" … Read More

  • [QUOTE=SMITA6076;1526498]So I just need to do the [ICODE]calendar.add( Calendar.DATE, 14 )[/ICODE] before the return statement rather than in it?[/QUOTE] It was just the wrong return type. You tried to return calendar.add(...) which isn't a String. Check [url]http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#add(int[/url], int) Read More

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)

0

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?

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

Edited by SMITA6076: n/a

1

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

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.