-2

My Program Java Calendar class is set to May-31

When I tried to get maximum no of days in May month, its returning 31. it's Ok.

int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Month:" + days);

But when i am trying to get Calendar.MONTH and Calendar.DAY_OF_MONTH

it is always returning June-1

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 3
    Can you update your question with more details, ideally the actual code that doesn't work for you? – maerics Jun 01 '11 at 02:25
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 24 '18 at 19:38

4 Answers4

4

Unlike all other Calendar fields, months are zero-based; January=0, February=1, etc. This means May is month number 4, not 5.

When you set the month to 5 and day to 31, you're trying to set it to June 31, which it coverts to July 1, there being only 30 days in June.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Since the code snippet prints 31, this is hardly the explanation of the observed behaviour. As you say, there are only 30 days in June. – Ole V.V. Jan 28 '18 at 21:38
0

tl;dr

Use the modern java.time classes that supplant the troublesome old legacy date-time classes.

YearMonth.from( 
    LocalDate.of( 2018 , Month.MAY , 31 ) 
).lengthOfMonth()

31

LocalDate.of( 2018 , Month.MAY , 31 ) 
         .getMonthValue()

5

LocalDate.of( 2018 , Month.MAY , 31 ) 
         .getYear()

2018

java.time

Your Question does not provide enough detail to diagnose the issue. But the question is moot, with a better alternative approach available now.

The troublesome Calendar class is now legacy, replaced by the java.time classes.

The modern approach to representing a month uses the YearMonth class.

YearMonth ym = YearMonth.of( 2018 , Month.MAY ) ;

Or get a YearMonth from a date, a LocalDate object.

LocalDate ld = LocalDate.of( 2018 , Month.MAY , 31 ) ;
YearMonth ym = YearMonth.from( ld ) ;

Interrogate for the length of the month.

int lengthOfMonth = ym.lengthOfMonth() ;

31

The LocalDate can give you numbers for its day-of-month and its year.

int dayOfMonth = ld.getMonthValue() ;
int year = ld.getYear() ;

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

getActualMaximum() returns the maximum value of the field.

  • getActualMaximum(Calender.DAY_OF_MONTH) returns 31, the maximum amount of days in a month
  • getActualMaximum(Calender.DAY_OF_WEEK) will return 7, the maximum amount of days in a week

Calendar.MONTH and Calendar.DAY_OF_MONTH are not automatically set to the date, so return their default values, June 1

Try

 Calendar c = Calendar.getInstance();

That should set it to today's date.

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
scientiaesthete
  • 919
  • 9
  • 20
-1

You just have to add one line after creating instance of Calendar().

For example:

Calendar cal = Calendar.getInstance();
cal.clear();

it will work.

Will
  • 24,082
  • 14
  • 97
  • 108
sudhir
  • 1