当前位置: 首页 > 面试题库 >

将天数添加到日历

蒙才
2023-03-14
问题内容

我是这个网站的新手,我才刚刚开始学习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;
       }

    }
}

感谢您阅读本文!我欢迎任何反馈…


问题答案:

这里有太多代码。用户互动过多。

从一种简单的方法开始做一件事,然后在正确的方法上解决。

这是您可能的操作方式:

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

一旦测试并证明了此方法,您就可以让其余的代码调用它。

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



 类似资料:
  • 问题内容: 我正在尝试写一些东西,让某人可以将有声读物上课,并且应该将到期日定为14天。我的班级有一个toString()方法,该方法应打印出到期日,但无论如何始终打印出到期日是3/5。 问题答案: 没有按照你的意思去做。它的值是静态常量的值,我想是2(实际上,您可以在源代码中看到它)。 我想你是说 此外,您不应该拨打两次电话:如果您拨打两次电话,日期可能会不一致。调用一次,将其分配给一个字段:

  • 为什么我在今天的基础上加上30天就得到了今天的30天,而当我加上20天就得到了今天的30天?? 这是一个样本 “这是控制台”

  • 问题内容: 我在MySQL中有一张表。将表中的当前日期值添加为2天的sql语句看起来像什么? 这会增加一秒钟的值,我不想更新时间,我想增加两天? 问题答案: 假设您的字段是一种类型(或相似类型): 使用您提供的示例,它看起来可能像这样: 这种方法也适用。

  • 问题内容: 我有一个日期,我想使用Python脚本为其添加5天。请考虑在月底也可以使用的一般解决方案。 我正在使用以下代码: ->正在打印 现在,我要在此日期之前增加5天。我使用以下代码: 哪个返回此错误: 问题答案: 先前的答案是正确的,但是通常这样做是更好的做法: 然后,您将拥有:

  • 我们正在使用API Google开发应用程序。在这个过程中,我们遇到了一些困难。 我们使用了php-sdk,在这个页面上“code . Google . com/p/Google-API-PHP-client/”我们使用了谷歌日历服务。我们遵循位于以下位置的文档:“developers . Google . com/Google-apps/calendar/v3/reference/”日历和事件部

  • 问题内容: 在某些情况下,我想将DATETIME格式的变量的值增加1天: 做这个的最好方式是什么? 问题答案: 如果要在PHP中执行此操作: 如果要在MySQL中添加日期: