0
How to create full month in java?
4 Antworten
+ 1
here's my solution
import java.util.Calendar;
.
.
.
.
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
//iterating from january 1st - january 31st
cal1.set(Calendar.MONTH,Calendar.JANUARY); //set month.
//the month value will be 0 - 11. 0 is januari.
//JANUARY is a const in CALENDAR. there're others month consts too
cal1.set(Calendar.DAY_OF_MONTH,1); //first day of month
cal2.set(Calendar.MONTH,Calendar.JANUARY);
cal2.add(Calendar.MONTH,1); // cal2 month value will be February
cal2.add(Calendar.DAY_OF_MONTH,-1); // feb 1st, -1 is january 31st
//iterate until end of month
while(cal1.get(Calendar.DAY_OF_MONTH) <= cal2.get(Calendar.DAY_OF_MONTH) ){
Date currentDate = cal1.getTime(); //get date value
//or if you want to take only its day
int day = cal1.get(Calendar.DAY_OF_MONTH); // it will has value from 1 - 31
cal1.add(Calendar.DAY_OF_MONTH,1);
}
0
what do you want to achieve? you want to iterate all days in a month?
0
yes
0
so does this will print whole calendar