Java Calendar reference - this gives some strange results

I am new to Java and am trying to do something with dates. I first started using the Date class, which I found out was mostly deprecated, so I switched to Calendar.

Now I am getting weird values. for example, the month value for December is 0, not 12. And on those Calendars where it gives me 0 for December, it also moves the year forward by the year.

This is strange!

What am I missing?

Thank you for your help.

-GG

EDIT EXAMPLE:

So I am reading a series of lines from a file, e.g .: Johnny Graham HF 12-2-1973 Black

I will parse it and then for the Calendar I installed:

int year = Integer.parseInt(stringVersionOfYear); // this value is 1973

      

Then later, when I go back with a line like this:

calendar.get(Calendar.YEAR)

      

value is 1974 ... And month is 0 for cal.get(Calendar.MONTH)

EDIT 2:

I am creating a Calendar like this:

Calendar outputCalendar = Calendar.getInstance();
outputCalendar.set(year, month, day);

      

+2


source to share


4 answers


Month values ​​- from 0 to 11; set the month to 12 and the date will be "normalized" by increasing the year by one and setting the month to 0. This will make it easier to "add the month" without worrying about year-end overflow.

EDIT: January = 0, February = 1, ... December = 11. When you set the month value to 12, you are asking for the 13th month, which has normalized to the first month of the next year.



Note that this normalization process happens at all - try setting the date to December 32 and you'll be back on January 1 of the next year. This means that it is important to be careful when modifying individual fields of the Calendar object. If you create a default calendar, say January 31st, and then want to change it to contain, for example, February 5th, the order in which you set the fields is important. If you change the month first, you create February 31st, which is normalized to March 2nd or 3rd (depending on the leap year), and then when you set the day to 5, the result is March 5th, not February 5th. You have the opposite problem on other occasions, for example starting from any date in February until the 30th or 31st of any other month. In this case, the first month leads to the same type of problem.

The only safe way to change a date is to use a method that sets all three values ​​at the same time, such as the set (int, int, int) method.

+3


source


The java.util.Date and Calendar classes are poorly designed (for example, the first day of the month is day 1, but the first month of the year is month 0). Many projects use Joda Time .



+5


source


See this line?

outputCalendar.set(year, month, day);

      

Just change it like this:

outputCalendar.set(year, month - 1, day);

      

and then when you want to get the month, don't use this:

cal.get(Calendar.MONTH)

      

use this:

(1+(cal.get(Calendar.MONTH)))

      

It's a pain, but it will fix the problem (I think).

0


source


The main tip I have for you: read the API! The calendar isn't really the pinnacle of intuitive API design, but it's definitely useful if you take the time to read the javadoc. Learn the difference between .add and .roll. Check what happens when you set the year (1973) to Calendar initialized with the current date (Calendar.getInstance by default).

Learning about the API is okay (we all do it), but in the end to find solutions, start by reading what the authors have provided you before asking the relatively stupid questions online.

0


source







All Articles