I want to increase a certain date by 1 day. I create a Calendar object like:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2012);
cal.set(Cal
DAY_OF_YEAR
Field number for get and set indicating the day number within the current year
DAY_OF_MONTH
Field number for get and set indicating the day of the month. This is a synonym for DATE
You will see difference if the day is greater than 31.
For adding it really makes no difference, but this
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.DAY_OF_YEAR));
prints
28
363
In addition of date it does not make any difference whether you use DAY_OF_MONTH or DAY_OF_YEAR. However, it makes sense when you get call getter and pass one of those.
You essentially advance the date by one, in both the cases. So there is no difference in both the approaches.
But sticking to a single method will render consistency across your codebase, maintainers will feel at home and probably the runtime optimizes the method call by compiling it as well.
Calendar.add Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
Here you have a list of the fields of Calendar that you can add or subtract:
MILLISECOND is the number of milliseconds between 0 and 999
SECOND is the number of seconds between 0 and 59
MINUTE is the number of minutes between 0 and 59
HOUR is the number of hours between 0 and 11
HOUR_OF_DAY is the number of hours between 0 and 23
DAY_OF_WEEK is the day in relation of the week between 1 and 7
DAY_OF_MONTH is the day in relation of the month between 1 and 31
DAY_OF_YEAR is the day in relation of the year between 1 and 365
WEEK_OF_MONTH is the week in relation of the month starting from 1
WEEK_OF_YEAR is the week in relation of the year starting from 1
MONTH is the month in relation of the year between 0 and 11
YEAR is the number of years starting from 1
Hours, days and weeks have multiple fields but it doesn't matter which one you choose1. For example using -8 for DAY_OF_WEEK
will work.
calendar.add(Calendar.DAY_OF_MONTH, -2); // subtract 2 days
calendar.add(Calendar.DAY_OF_WEEK, -2); // subtract 2 days
calendar.add(Calendar.DAY_OF_YEAR, -2); // subtract 2 days
calendar.add(Calendar.YEAR, -2); // subtract 2 years
1It doesn't matter only using Calendar.add
, with other operations the results might be different.
Actually, there can and will be a difference depending on what field type you choose:
* http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
Usage model.
To motivate the behavior of add() and roll(), consider a user interface component with increment and decrement buttons for the month, day, and year, and an underlying GregorianCalendar. If the interface reads January 31, 1999 and the user presses the month increment button, what should it read? If the underlying implementation uses set(), it might read March 3, 1999. A better result would be February 28, 1999. Furthermore, if the user presses the month increment button again, it should read March 31, 1999, not March 28, 1999. By saving the original date and using either add() or roll(), depending on whether larger fields should be affected, the user interface can behave as most users will intuitively expect.