import java.util.GregorianCalendar;
public class CalendarMain {
public static void main(String[] args) {
GregorianCalendar calendar = new GregorianCalendar();
int month = calendar.get(GregorianCalendar.MONTH)+1;
int year = calendar.get(GregorianCalendar.YEAR);
int weekday = calendar.get(GregorianCalendar.DAY_OF_WEEK);
int dayOfMonth = calendar.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println(month+"/"+dayOfMonth+"/"+year);
calendar.add(GregorianCalendar.DAY_OF_MONTH, 10);
System.out.println(GregorianCalendar.DAY_OF_MONTH);
}
}
我正在尝试将10天添加到当前日期,但我遇到了一个奇怪的问题。它似乎没有正确添加。
输出
2014年9月18日
5
答案 0 :(得分:3)
// Get a calendar which is set to a specified date.
Calendar calendar = new GregorianCalendar(2014, Calendar.JANUARY, 1);
// Get the current date representation of the calendar.
Date startDate = calendar.getTime();
// Increment the calendar's date by 1 day.
calendar.add(Calendar.DAY_OF_MONTH, 1);
// Get the current date representation of the calendar.
Date endDate = calendar.getTime();
System.out.println(startDate);
System.out.println(endDate);
答案 1 :(得分:1)
我认为最后一行是错误的,试试这个:
System.out.println(calendar.get(GregorianCalendar.DAY_OF_MONTH));
答案 2 :(得分:1)
使用
System.out.println(calendar.get(GregorianCalendar.DAY_OF_MONTH));
而不是
System.out.println(GregorianCalendar.DAY_OF_MONTH);
输出
9/18/2014
28
您在代码中所做的是打印GregorianCalendar.DAY_OF_MONTH
最终变量的整数代码,无论您在calendar
中添加了什么,它都将保持 5 。您需要使用calendar.get(...)
函数来获取此日历的月份日期。