Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between adding DAY_OF_MONTH or DAY_OF_YEAR to a Calendar object?

Tags:

java

datetime

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(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 31); 

Then, for increasing it by 1 day, I can do 2 things :

cal.add(Calendar.DAY_OF_MONTH, 1); 

OR

cal.add(Calendar.DAY_OF_YEAR, 1); 

There are also other "DAY" constants, but I get the same result using the above 2 methods of increasing the day by 1. In which case will I get different results for the two?

like image 924
Daud Avatar asked Dec 28 '12 05:12

Daud


People also ask

What is calendar Day_of_year in Java?

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.

What is the use of calendar getInstance () in Java?

Calendar 's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time: Calendar rightNow = Calendar.


2 Answers

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 
like image 86
Evgeniy Dorofeev Avatar answered Sep 24 '22 03:09

Evgeniy Dorofeev


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 366

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

like image 21
Paolo Forgia Avatar answered Sep 22 '22 03:09

Paolo Forgia