how to get minimum and maximum date from given month in java using java.util.Calendar.
Asked
Active
Viewed 1.5k times
5 Answers
5
The minimum is always the 1st of this month. The maximum can be determined by adding 1 to month and subtracting 1 from the Calendar day field.

stacker
- 68,052
- 28
- 140
- 210
-
thanks for the help, i got the solution as, public void ddl_month_valueChange(ValueChangeEvent event) { int v_month = Integer.parseInt(event.getNewValue().toString()) - 1; java.util.Calendar c1 = java.util.Calendar.getInstance(); c1.set(2011, v_month, 1); Date d_set_att_from = c1.getTime(); cal_att_from_date.setValue(d_set_att_from); c1.add(java.util.Calendar.MONTH, 1); c1.add(java.util.Calendar.DATE, -1); Date d_set_att_to = c1.getTime(); cal_att_to_date.setValue(d_set_att_to); } – Annu Apr 12 '11 at 05:49
-
in which cal_att_from_date and cal_att_to_date is the object of org.primefaces.component.calendar.Calendar – Annu Apr 12 '11 at 05:49
-
@Annu, is this solution easier than just call one method? – oiavorskyi Apr 12 '11 at 05:50
3
This could be done this way:
c = ... // get calendar for month you're interested in
int numberOfDays = c.getActualMaximum(Calendar.DAY_OF_MONTH)
You could find minimum and maximum value the same way for any of components of the date.

oiavorskyi
- 2,893
- 1
- 20
- 23
1
Have you tried the following?
After setting your calendar object to your desired month,
calendar.getActualMaximum(Calendar.DATE);
For the minimum, I suppose it's always the first.
Hope that helps.

Ervi B
- 770
- 7
- 16
1
Minimum date is always 1 and Maximum date can be calculate as
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
int year = 2010;
int month = Calendar.FEBRUARY;
int date = 1;
int maxDay =0;
calendar.set(year, month, date);
System.out.println("First Day: " + formatter.format(calendar.getTime()));
//Getting Maximum day for Given Month
maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(year, month, maxDay);
System.out.println("Last Day: " + formatter.format(calendar.getTime()));
Hopefully this will helps

Imran
- 2,906
- 1
- 19
- 20
0
I got solution as below,
public void ddl_month_valueChange(ValueChangeEvent event) {
int v_month = Integer.parseInt(event.getNewValue().toString()) - 1;
java.util.Calendar c1 = java.util.Calendar.getInstance();
c1.set(2011, v_month, 1);
Date d_set_att_from = c1.getTime();
cal_att_from_date.setValue(d_set_att_from);
c1.add(java.util.Calendar.MONTH, 1);
c1.add(java.util.Calendar.DATE, -1);
Date d_set_att_to = c1.getTime();
cal_att_to_date.setValue(d_set_att_to); }

Annu
- 532
- 4
- 8
- 22