Java 将天添加到日历
我是这个网站的新手,我刚刚开始学习Java。我想给GregoriaCalendar加上几天,但没用。在这里(忽略顶部块),底部的添加日期令人讨厌Java 将天添加到日历,java,date,gregorian-calendar,Java,Date,Gregorian Calendar,我是这个网站的新手,我刚刚开始学习Java。我想给GregoriaCalendar加上几天,但没用。在这里(忽略顶部块),底部的添加日期令人讨厌 /* * 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
/*
* 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;
}
}
}
/*
*作者:马特M
*日期:8.12.13
*说明:当用户输入截止日期和项目难度时,
*程序给出了他开始工作的日期
*/
导入java.util.*;
公共类死线规划器{
公共静态void main(字符串[]args)
{
//获取信息,如果信息错误,则重新开始提问
整月=0,日=0;
做
{
做
{
System.out.println(“请输入月份”);
月份=(新扫描仪(System.in).nextInt()-1);
System.out.println(“请输入日期”);
day=(新扫描仪(System.in).nextInt());
}
而(!(月=0));
}
而(!(天=0));
//制作新的日历并初始化它
GregorianCalendar设置=新的GregorianCalendar();
设置。设置(2013年,月,日);
System.out.println(“截止日期是”+setup.getTime());
//switch语句提供开始日期
System.out.println(“项目是难还是容易?”);
扫描仪难度=新扫描仪(System.in);
开关(难易性.nextLine())
{
案例“简单”:
设置。添加(第-1天);
System.out.print(“您应该开始工作的日期是”);
System.out.println(setup.getTime());
打破
案例“硬”:
添加(第-10天);
System.out.print(“您应该开始工作的日期是”);
System.out.println(setup.getTime());
打破
违约:
System.out.println(“您对问题的回答不正确”);
打破
}
}
}
谢谢你通读这篇文章!我愿意接受任何反馈 这里的代码太多了。用户交互太多 从一个简单的方法开始做一件事,然后在你做对了之后找到解决办法 以下是您可能的做法:
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
您需要更改如下行:
setup.add(day, -1);
setup.add(day, -10);
到
有关更多信息,请参阅。格里高利日历有它自己的价值,您应该使用它来告诉它您正在增加什么 你在说什么
setup.add(day, -1);
您应该使用公历日历值作为日期
setup.add(Calendar.DAY_OF_MONTH, -1);
有关日历及其字段的更多信息可在此处找到
也试着看看这篇过去的帖子:
非常感谢,您的回答非常有用。是否可以使用所有的“DAY\u OF\u X”选项?对不起,我不知道这是什么。您可以从日历javadocs中定义的公共静态常量中进行选择;就这样。最好从JUnit测试开始。
setup.add(Calendar.DAY_OF_MONTH, -1);
Calendar c = new GregorianCalendar(2000, Calendar.JANUARY, 1);
c.add(Calendar.DAY_OF_MONTH, 1);