Adding Days to Calendar
Asked Answered
J

4

7

I'm new to this site and I have just started learning Java. I'm trying to add couple days to the GregorianCalendar but it doesn't work. Here... (Ignore the top chunk), its the adding dates at the bottom that is annoying.

/*
 * Author:Matt M
 * Date:8.12.13
 * Discription: When the user inputs the deadline, and the difficulity of the project, 
 * the program gives the date he should start working on it 
 */
import java.util.*;
public class DeadlinePlanner{
    public static void main(String[] args)
    {
        //take information and restart questions if information is wrong 
        int month = 0, day = 0 ; 
        do
        {
        do
        {
        System.out.println("Input the month please");
        month = (new Scanner(System.in).nextInt() - 1);
        System.out.println("Input the day please");
        day = (new Scanner(System.in).nextInt());

        }
        while (!(month <= 12) || !(month >= 0));
        }
       while (!(day <= 31) || !(month >= 0));

       //Make new calender and initialize it 
       GregorianCalendar setup = new GregorianCalendar();
       setup.set(2013, month, day);
       System.out.println("The deadline is "+ setup.getTime());

       //switch statement to give starting date
       System.out.println("Is the project hard or easy?");
       Scanner difficulity = new Scanner(System.in);

       switch (difficulity.nextLine())
       {

           case "easy":


               setup.add(day, -1);
               System.out.print("The date you should start workinng on is ");
               System.out.println(setup.getTime());
               break;
           case "hard":

               setup.add(day, -10);
               System.out.print("The date you should start workinng on is ");
               System.out.println(setup.getTime());

               break;
           default:
               System.out.println("Your answers to the questions are incorrect");
               break;
       }

    }
}

Thanks for reading through this!I'm open to any feedback...

Jocund answered 12/8, 2013 at 22:28 Comment(0)
J
20

There's too much code here. Too much user interaction.

Start with a simple method to do one thing, then work your way out after you get that right.

Here's how you might do it:

public class DateUtils {
    private DateUtils() {}

    public static Date addDays(Date baseDate, int daysToAdd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(baseDate);
        calendar.add(Calendar.DAY_OF_YEAR, daysToAdd);
        return calendar.getTime();
    }
}

Once you have this method tested and proven you can let the rest of you code just call it.

UPDATE: It's four years later, and JDK 8 has given us the new JODA-based time package. You should be using those classes, not the JDK 1.0 Calendar.

Jackfruit answered 12/8, 2013 at 22:33 Comment(3)
Is it possible that using all "DAY_OF_X" is ok?Waaf
Sorry, I don't know what that is. You can choose from the public static constants defined in the Calendar javadocs; that's it.Jackfruit
Even better start with a JUnit test.Purely
R
1

You need to change the lines that look like:

setup.add(day, -1);
setup.add(day, -10);

to

setup.add(GregorianCalendar.DAY_OF_MONTH, -1);
setup.add(GregorianCalendar.DAY_OF_MONTH, -10);

See GregorianCalendar for more information.

Rachelrachele answered 12/8, 2013 at 22:34 Comment(0)
J
1

Gregorian calander has its own value you should be using to tell it what you are increasing where you are saying

setup.add(day, -1);

you should use the Gregorian calander value for day

setup.add(Calendar.DAY_OF_MONTH, -1);
Junji answered 12/8, 2013 at 22:35 Comment(0)
O
0
Calendar c = new GregorianCalendar(2000, Calendar.JANUARY, 1);
c.add(Calendar.DAY_OF_MONTH, 1);

More info on Calendar and its fields can be found here Calendar

Also try to look at this past post: here

Obey answered 12/8, 2013 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.