2

I have a calendar initially set to 2019-11-01, that i want to set to the first date and the last date of month using the:

cal.set(int field,int value) 

And for the field i use either:

Calendar.DATE or Calendar.DAY_OF_MONTH

But system on sysout shows that it is setting the Calendar.YEAR instead

System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));

cal.set(cal.get(Calendar.DAY_OF_MONTH),1);
        
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
        
cal.set(cal.get(Calendar.DAY_OF_MONTH),cal.getActualMaximum(Calendar.DAY_OF_MONTH));

System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));

Sysout:

2019-11-01

0001-11-01

0030-11-01

Community
  • 1
  • 1
kyrpav
  • 756
  • 1
  • 13
  • 43
  • 2
    Java 8, with its java.time API, came out in March 2014. We're in November 2019, more than 5 years later. Stop using te old, obsolete, clunky Calendar and Date classes. Use the java.time API. – JB Nizet Nov 17 '19 at 23:28
  • 1
    `cal.set(Calendar.DAY_OF_MONTH, 1);` and `cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));` - do not call `cal.set(cal.get(Calendar.DAY_OF_MONTH)` - that is not correct. – Elliott Frisch Nov 17 '19 at 23:29
  • Somehow a variant of [date and time not working in Alarm Manager](https://stackoverflow.com/questions/57743785/date-and-time-not-working-in-alarm-manager) – Ole V.V. Nov 18 '19 at 04:44
  • 2
    Yes, the `Calendar` class has a confusing interface. No wonder people are making such errors. You’re far from the first and not the last either, unfortunately. The good solution (and it is *very* good) is to stop using the `Calendar` class and start using [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. – Ole V.V. Nov 18 '19 at 04:48
  • 1
    thank you all i will quit this Calendar class – kyrpav Nov 18 '19 at 08:13
  • Congratulations on the decision to quit the `Calendar` class. You may consider helping other readers to do the same by accepting the answer that recommends just that? – Ole V.V. Nov 19 '19 at 21:45

2 Answers2

5

You can use the java-8 modern date time API LocalDate, stop using the legacy Calendar or util.Date

LocalDate date = LocalDate.parse("2019-11-01");

System.out.println(date.with(TemporalAdjusters.firstDayOfMonth()));  //2019-11-01
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth()));   //2019-11-30
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

you should use

Calendar.DAY_OF_MONTH

NOT

cal.get(Calendar.DAY_OF_MONTH)

since the Calendar class have static fields to represents the ID of each field, for example DAY_OF_MONTH = 5 and when you use cal.set(cal.get(Calendar.DAY_OF_MONTH),1) you told the calendar to get the calendar value of DAY_OF_MONTH which is equal to = 1 (you said the cal value = 2-19-11-01), and 1 is the Year, so that you get the year set to 1 instead of the day

use it like: cal.set(Calendar.DAY_OF_MONTH,1);

YoHaRo
  • 142
  • 2
  • 7