简体   繁体   中英

Setting values in calendar (java.util.Calendar)

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM