Does the setMonth Java Calendar method not work?

I have a little code like below. I expected the result to be 7 , but it printed 6 . If I uncomment the tmp.get(Calendar.MONTH) line tmp.get(Calendar.MONTH) , it works fine (prints 7 ).

Please let me know the reason. I am using JDK 1.7.0_25 on macOS.

 public static void main(String[] args) { Calendar tmp = Calendar.getInstance(); tmp.set(Calendar.DAY_OF_MONTH, 4); tmp.set(Calendar.MONTH, Calendar.AUGUST); //tmp.get(Calendar.MONTH); tmp.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(tmp.get(Calendar.MONTH)); } 

Screenshot:
Comment Code: http://gyazo.com/4c099b1b2b90d72d1954b98b134e4ac3
Uncomment code: http://gyazo.com/fe368745da168646140ca9f3a60d2021

+5
source share
5 answers

Since the month index starts at index 0. add +1 at the time the month is received. this is a c-structure copied in java. It has indices from 0 to 11.

And I think the day of the month is wrong. comment on this and run it, showing correctly. ( tmp.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); )

  tmp.set(Calendar.DAY_OF_MONTH, 4); tmp.set(Calendar.MONTH, Calendar.AUGUST); //tmp.get(Calendar.MONTH); //tmp.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(tmp.get(Calendar.MONTH)); 

The default is 2015 and August 4th is tuesday not monday

+3
source

I tried to run your code and it prints 7 each time (even tried to uncomment this line). What seems right to me.

EDIT Problem explained in detail Java Calendar - Date unpredictable after setting day_of_week

I'm glad that with Java 8 a new API for working with date and time has appeared.

EDIT2

My understanding of what happened:

Only reasonable combinations

 YEAR + MONTH + DATE (let call it A) 

or

 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK (let call it B) 

And the calendar has its internal state in which he thinks. Therefore, before installing

 DAY_OF_WEEK 

you worked with combination A, but after that you worked with combination B and this line

 tmp.set(Calendar.DAY_OF_MONTH, 4); 

was ignored. Thus, this happened on Monday in the first week of August (2015-07-27). But when did you call

 tmp.get(Calendar.MONTH); 

you “materialized” the date in accordance with combination A (until 2015-08-04) and

 DAY_OF_WEEK 

has been installed "correctly" since 2015-08-04.

Anyway, could you try this with JDK 8?

+2
source

When you set DAY_OF_WEEK = MONDAY, the calendar returns on the last Monday and July 27, you can check it using System.out.println(tmp.get(Calendar.DAY_OF_MONTH));

+1
source

Avoid Pre-Java-8 Date and Calendar

Existing date and time Java classes are bad, mutable, and have unpredictable performance.

This applies somehow equally to the Calendar, which has some of the same problems. Calendar weaknesses (German link)

  • not calendar, but date + time
  • mutable
  • January 0 month, not 1 st
  • unintuitive API: you should use get(Calendar.HOUR) instead of getHour
  • ...

If you are using Java <8, use Joda-Time

The comments on java-calendar-date-is-unpredictable-after-setting-day-of-week recommend Joda-Time as an alternative to @JonSkeet, as well as those JDK Enhancement Proposal people . Your code may look like

 import org.joda.time.MutableDateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class Test2 { public static void main(String[] args) { MutableDateTime tmp = new MutableDateTime(); tmp.setDayOfMonth(4); tmp.setMonthOfYear(8); tmp.setDayOfWeek(1); DateTimeFormatter fmt = DateTimeFormat.fullDate(); System.out.println(fmt.print(tmp)); } } 

Unchanging

There is also an immutable DateTime class that you want to use . (immutability adds several positive effects, in Java 8 it is by default )

 import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class Test { public static void main(String[] args) { DateTimeFormatter fmt = DateTimeFormat.fullDate(); DateTime tmp = new DateTime().withDayOfMonth(4).withMonthOfYear(8); System.out.println(fmt.print(tmp)); tmp = tmp.withDayOfWeek(1); System.out.println(fmt.print(tmp)); } } 

If on Java-8,

The java.time API java.time based on Joda-Time. Use

 import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Test { public static void main(String[] args) { DateTimeFormatter fmt = DateTimeFormatter.RFC_1123_DATE_TIME; LocalDateTime tmp = LocalDateTime.now().withDayOfMonth(4).withMonthOfYear(8); System.out.println(fmt.format(tmp)); tmp = tmp.withDayOfWeek(1); System.out.println(fmt.format(tmp)); } } 

(I have not tested the version of Java-8)

0
source

When you set the day of the week, the Calendar is not able to find out what you expect. If you expect to receive the date of another day of the same week, you should also indicate the week of the week to indicate which week you are expecting.

0
source

All Articles