2

I'm learning the Calendar class in Java and I am unable to understand the Set(Calendar.Day OF MONTH) method.

Here it goes:

import java.util.Calendar;  
import java.util.Date


public class TestCalender
{

 public static void main(String[] args)
    {

        Calendar cal = Calendar.getInstance();
        Date date= cal.getTime();
        System.out.println(date);
        cal.set(Calendar.DAY_OF_MONTH,33);
        //cal.set(Calendar.MONTH,13);------>(1)
        Date newdate = cal.getTime();
        System.out.println(newdate);

Output:

Fri May 12 17:30:50 CDT 2017  
Fri Jun 02 17:30:50 CDT 2017

When I uncomment the statement(1) the output changes to:

Fri May 12 17:33:22 CDT 2017  
Mon Mar 05 17:33:22 CST 2018

Here is my question:

I understood the change of Month to March but I'm not able to figure out why the date has changed to 5. As per my understanding shouldn't the date be changed to April 02 2018 (when 33 days of March is being computed since March has only 31 days the count moves to the month of April).

I will be extremely grateful if someone could help in clearing this doubt.

Thanks in advance.

Regards Roopa

Steven
  • 1,236
  • 1
  • 13
  • 37
Roopa
  • 23
  • 5
  • You are computing the 33rd day of **MAY** (not March). – Elliott Frisch May 12 '17 at 22:52
  • 5
    *I am learning the Calendar class*: really, you shouldn't. Calendar is awfully designed, and obsolete. Learn the classes in the java.time package. – JB Nizet May 12 '17 at 22:53
  • @JBNizet unfortunately most Java developers get asked to maintain legacy code, which we have to understand. So, go ahead and learn the `Calendar` class, but only for the purpose of understanding it; don't use it yourself. – Klitos Kyriacou May 12 '17 at 23:28
  • @KlitosKyriacou true. But maintaining existing code does not see to be the goal here. – JB Nizet May 12 '17 at 23:31
  • @JBNizet Iam just trying to get a better view of the Calendar i would be moving to studying java.util.time class next. – Roopa May 12 '17 at 23:48
  • @JBNizet Thanks for your input. – Roopa May 12 '17 at 23:49
  • @Roopa No need for `Calendar` at all. No benefit to studing that class as it will only rot your mind and misguide you with its poor approaches to class design and OOP. The java.time classes are intended to *entirely* replace the `Calendar` & `Date` classes. – Basil Bourque May 13 '17 at 02:17
  • @KlitosKyriacou Regarding maintaining existing code, I disagree about needing to learn `Calendar`. The old classes were given new conversion methods to move to/from java.time classes. Any code being added or modified can be done in java.time with the inputs and results being converted to/from the legacy types. No need to learn how to fix the [Yugo](https://en.wikipedia.org/wiki/Zastava_Koral) when you have a Honda standing-by. – Basil Bourque May 13 '17 at 02:20

2 Answers2

4

The Calendar class uses months starting from 0, and ending at 11 for December. Therefore, when you set the month to 13, you specified February of the following year, and the "33rd day of February" (which has 28 days) is the 5th of March.

The java.util.date classes are quirky and hard to use. Use java.time instead.

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • Please correct me if Im wrong . so first the month is ascertained and after that the days are calculated based on the new month. – Roopa May 12 '17 at 23:46
  • @Roopa effectively yes, but what is actually happening is that the Calendar object holds the values of individual fields separately, so the day of month is 33, and the month is February. When you then try to do calculations with it (such as get extract the whole date) it then tries to make sense of its values, and decides that means 5 March. In fact, such values should have resulted in an invalid state, but the designers of the class chose to allow it. – Klitos Kyriacou May 13 '17 at 10:43
  • Thanks a ton i got this now. Appreciate it !! – Roopa May 14 '17 at 02:37
2

I'm learning the Calendar class

Don't.

The Calendar class is notoriously troublesome, poorly designed, confusing, and flawed. Now legacy. Supplanted by the java.time classes. We can sweep that class into the dustbin of Java history.

Among its many issues, Calendar uses crazy month numbering 0-11 for January-December. This fact was correctly described in the correct Answer by Kyriacou. The java.time classes, in contrast, use sane numbering 1-12 for Jan-Dec; see the Month enum.

Not quite sure what your goal is in that code snippet, but you seem to be adding 33 days to a date.

The LocalDate class represents a date-only value without time-of-day and without time zone.

One big difference between java.time and the legacy classes is that the modern classes use immutable objects. So adding days to a date results in a new date object with its values based on the original object, while the original object remains untouched. This avoids much confusion, and makes them thread-safe.

LocalDate ld = LocalDate.of( 2017 , Month.MARCH , 23 ) ;
LocalDate later = ld.plusDays( 33 );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Good answer, but note the OP was not trying to add 33 days, but trying to _set_ the day-of-month to an absolute value of 33. – Klitos Kyriacou May 13 '17 at 10:40
  • @KlitosKyriacou And why would he/she do that? What is the point of that? If the Question were edited to reveal the true purpose of its author, I would be happy to edit my example code to suit. Perhaps we should have closed the Question for being senseless. – Basil Bourque May 13 '17 at 15:44
  • @BasilBourque Adding 33 days was actually part of an example from the book Khalid A Mughal. – Roopa May 14 '17 at 02:39
  • 1
    @Roopa Adding 33 days, and setting the day-of-month to 33, are two very different things. – Basil Bourque May 23 '19 at 03:35