将日历添加到日历 [英] Adding Days to Calendar

查看:160
本文介绍了将日历添加到日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Java。我试图添加几天到GregorianCalendar,但它不起作用。在这里...(忽略顶部的块),它的添加日期在底部是恼人的。

  / * 
*作者:Matt M
*日期:8.12.13
*说明:当用户输入截止日期和项目的难度时,
*程序给出应该开始工作的日期
* /
import java.util。*;
public class DeadlinePlanner {
public static void main(String [] args)
{
//获取信息并重新启动问题,如果信息错误
int month = 0 ,day = 0;
do
{
do
{
System.out.println(输入月份);
month =(new Scanner(System.in).nextInt() - 1);
System.out.println(输入日请);
days =(new Scanner(System.in).nextInt());

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

//创建新的日历并初始化
GregorianCalendar setup = new GregorianCalendar();
setup.set(2013,月,日);
System.out.println(截止日期是+ setup.getTime());

// switch语句给出开始日期
System.out.println(项目是硬还是容易?);
扫描仪difficulity =新扫描仪(System.in);

switch(difficulity.nextLine())
{

caseeasy:


setup.add天,-1);
System.out.print(你应该开始工作的日期是);
System.out.println(setup.getTime());
break;
casehard:

setup.add(day,-10);
System.out.print(你应该开始工作的日期是);
System.out.println(setup.getTime());

break;
默认值:
System.out.println(您的问题答案不正确);
break;
}

}
}

感谢阅读这个!我可以接受任何反馈...

解决方案

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



开始一个简单的方法来做一件事情,然后在你得到正确之后,你的出路。



你可以这样做:

  public class DateUtils {
private DateUtils(){}

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

一旦你有这种方法测试,证明你可以让其余的代码只是调用它。


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

解决方案

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.

这篇关于将日历添加到日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆