0

How to create full month in java?

14th Nov 2016, 11:43 AM
Dhananjay Patil
Dhananjay Patil - avatar
4 Respuestas
+ 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); }
14th Nov 2016, 12:33 PM
Raizal I N Pregnanta
Raizal I N Pregnanta - avatar
0
what do you want to achieve? you want to iterate all days in a month?
14th Nov 2016, 11:50 AM
Raizal I N Pregnanta
Raizal I N Pregnanta - avatar
0
yes
14th Nov 2016, 11:57 AM
Dhananjay Patil
Dhananjay Patil - avatar
0
so does this will print whole calendar
14th Nov 2016, 1:49 PM
Dhananjay Patil
Dhananjay Patil - avatar