我有一个课程的作业,我们在这个课程中考虑日期,然后查看给定的日期是否有效。如果是的话,我们就加上一天。我遇到麻烦的地方是在我检查日期有效后将其保存到
Date appointment = new Date();
appointment = new Date(month, day, year);
Date.advanceDate(appointment);
在另一个名为Date.java的文件中
public static void advanceDate(Date aDate){
//here is where I need to read the date in aDate
}
通过Java api在线搜索后,我找不到在约会上添加一天的方法,或者从约会中获取一天,一个月和一年,每天添加一天,然后将其另存为新日期
我在尝试查看http://docs.oracle.com/javase/8/docs/api/java/util/Date.html
后所做的尝试aDate.getDay();
但是eclipse告诉我“方法getDate()未定义类型Data
aDate.toString();
这不会返回它在memery中返回其位置的月份日期和年份
我在网上找到的每个解决方案都使用了Calender
,似乎已经取代了日期
答案 0 :(得分:0)
我相信您正在寻找Calendar
和DateFormat
,
int year = 2015;
int month = Calendar.FEBRUARY; // <-- this is actually a 1.
int date = 8;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(df.format(cal.getTime()));
输出
2015-02-09
1 FEBRUARY