5 Replies Latest reply: Mar 31, 2008 2:55 AM by masijade RSS

    Adding days to the current date

    807591
      I'm trying to write a program that takes an order and prints an invoice for my intro to java class. The date of the order and the date of arrival (which is 14 days after the date of the order) are to appear on this invoice. I thought that I figured it out but I get a compile error. Here's what I have so far:
       SimpleDateFormat formatDate = new SimpleDateFormat("MMMMM, dd, yyyy");
          
          Calendar rightNow = Calendar.getInstance();
          
          public String dateOfOrder()
          {
               return formatDate.format(rightNow);
          }
          
          public String dateOfArrival()
          {
               return formatDate.format(rightNow.add(DAY_OF_MONTH, 14));
          }
      I'm writing it in JCreator and I get "cannot find symbol variable DAY_OF_MONTH" as my compile error. It compiled just fine until I added the dateOfArrival method. I keep reading through the Calendar API but I just can't understand why it won't work. What am I doing wrong?
        • 1. Re: Adding days to the current date
          masijade
          Which class is "DAY_OF_MONTH" a part of (I know what it is, but I want you to think on it). Try adding that class name to the front of the field name, with a period between them i.e.
          CLASS.FIELD
          that is how you access a class' field value (or with a static import, but we don't want to get into those).
          • 2. Re: Adding days to the current date
            807591
            masijade. wrote:
            Which class is "DAY_OF_MONTH" a part of (I know what it is, but I want you to think on it). Try adding that class name to the front of the field name, with a period between them i.e.
            Calendar?

            I tried making the following adjustment but just got a new error.
            public String dateOfArrival()
                {
                     return formatDate.format(rightNow.add(Calendar.DAY_OF_MONTH, 14));
                }
            My error this time is: 'void' type not allowed here

            It seems like it is recognizing DAY_OF_MONTH now but something else is going wrong. Where am I going wrong?
            • 3. Re: Adding days to the current date
              masijade
              See the API documentation.

              What is the return type of the add method? What does it do? Should that method be used in the format method call? Or, should you do the add method call then call something else in the the format method call (HINT such as a method that returns a Date object, and there is one in Calendar and the API docs will tell you what it is)?
              • 4. Re: Adding days to the current date
                807591
                Honestly, I am not sophisticated enough to follow your last post. We really haven't covered anything like this in my class. These methods are < 5% of what the project calls for us to do in total and at least half of what we are expected to know is not covered by either the text book or the lectures. I don't understand why learning a programming language has to be a giant game of where's Waldo. I wouldn't complain if I were teaching myself but what am I paying the tuition for?

                Anyway, thanks for trying to help but I feel more lost than when I started. I think that's a sign I need to go to bed.
                • 5. Re: Adding days to the current date
                  masijade
                  FYI:

                  This is the API documentation

                  http://java.sun.com/javase/6/docs/api/index.html