Java 向日历添加天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18197710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:12:55  来源:igfitidea点击:

Adding Days to Calendar

javadategregorian-calendar

提问by user2676580

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.

我是这个网站的新手,我刚刚开始学习 Java。我正在尝试向 GregorianCalendar 添加几天,但它不起作用。这里......(忽略顶部的块),底部的添加日期很烦人。

/*
 * 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...

感谢您阅读本文!我愿意接受任何反馈...

采纳答案by duffymo

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, notthe JDK 1.0 Calendar.

更新:四年后,JDK 8 为我们提供了新的基于 JODA 的时间包。您应该使用这些类,而不是JDK 1.0 Calendar

回答by Brinnis

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 GregorianCalendarfor more information.

有关更多信息,请参阅GregorianCalendar

回答by Sean F

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

Gregorian calander 有它自己的价值,你应该用它来告诉它你在说什么

setup.add(day, -1);

you should use the Gregorian calander value for day

你应该使用公历日历值作为一天

setup.add(Calendar.DAY_OF_MONTH, -1);

回答by Susie

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

也试着看看这个过去的帖子: 这里