-2
I have this code:
import javax.swing.JOptionPane;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
import java.text.*;

    public class BillionSeconds {

        public static void main(String[] args)
        {
            Date thedate ;


            String Birthday = JOptionPane.showInputDialog("What is your birthday in the form dd-MM-yy");

            DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yy");
            try{
            thedate = dateFormat.parse(Birthday);
            }
            catch (Exception e) {
                System.out.println("Unable to parse date stamp");
            }
            Date newdate = thedate.add(thedate, 1);
        }
    }

But I get this error and I cant figure out why:

error: cannot find symbol method add(Date,int)
eboix
  • 5,113
  • 1
  • 27
  • 38
mike628
  • 45,873
  • 18
  • 40
  • 57
  • 2
    Where in the [Date API](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html) is there an `add(...)` method? This question could have been answered by you with a 2 second scan of the API. – Hovercraft Full Of Eels Aug 30 '12 at 17:32
  • WHy have you tagged this question with "exception" when it's a compile-time error? – Jon Skeet Aug 30 '12 at 17:37
  • And if it did have an add method what would `add(thedate, 1)` add to? Add one to the year, month, day? I suggest to google "java date add". One of the hits is this http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – km1 Aug 30 '12 at 17:40

3 Answers3

0

As it says, there is no add method in java.util.Date. You might want to take a look at GregorianCalendar. It has intelligent methods like you need. Or even better, use the third-party library JodaTime.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

add(thedate, 1);

There is an add() method in Calendar Class not Date class....

Eg:

Calendar desiredDate = toDay.add(Calendar.DATE, 4);

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Yup, that's because Date doesn't have an add method. What made you think it did?

It sounds like you might be thinking of the Calendar class, although then you'd want:

Calendar nextDay = currentDay.add(Calendar.DATE, 1);

... which isn't quite the same thing.

I would strongly recommend that you abandon Date and Calendar entirely though, and instead start using Joda Time, which is a much, much better date/time API.

Note that you should also get a compile-time error stating that thedate may not have been initialized, due to your "catch and continue" error handling.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194