Print dates for all mondays and fridays in a month?

I am trying to use Calendar to get all weekdays and their dates for a month. Then I would like to print dates for all Mondays and Fridays for a random month, is that possible?
[182 byte] By [fedevapsa] at [2007-11-27 4:17:54]
# 1

Sure.

One option is to iterate through the days of the month, for each day test whether it's a Monday or Friday, if it is, print in.

A bit more complicated, test the weekday of the first of the month, determine how many days to add to get to the first Monday or Friday in the month. Every time you have a Monday, add four days to get to Friday. On every Friday, add three days to get to Monday. When in next month, stop.

If you run into problems, please post again.

OleVVa at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 2
But how do I iterate through the days in a month in the Calendar? I have made a Calendar object like:Calendar rightNow = Calendar.getInstance(); But its not possible to specify an iterator for this object.
fedevapsa at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 3
Use this to go go one day forward:rightNow.add(Calendar.DAY_OF_MONTH, 1);(Not tested, please correct any errors; you will want to call the variable something else)
OleVVa at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 4
This code:Calendar rightNow = Calendar.getInstance();int dayofmonth = rightNow.DAY_OF_MONTH;System.out.println(dayofmonth);prints 5 eventhough we have the 14 of may. Do I have to initialize it with the year or something?
fedevapsa at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 5

> This code:

>

> > Calendar rightNow = Calendar.getInstance();

> ce();

>int dayofmonth = rightNow.DAY_OF_MONTH;

>System.out.println(dayofmonth);

>

>

> prints 5 eventhough we have the 14 of may

>. Do I have

> to initialize it with the year or something?

No, but you have to use it properly. DAY_OF_MONTH is a constant.

You want to use a method that gets the field you want (specified with one of the constants.

int dayofmonth = rightNow.get(Calendar.DAY_OF_MONTH);

cotton.ma at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok so this:

Calendar rightNow = Calendar.getInstance();

// Number of days in this month:

int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);

// date in current month:

int date = rightNow.get(Calendar.DAY_OF_MONTH);

// day in current week:

int day = rightNow.get(Calendar.DAY_OF_WEEK);

System.out.println(days + "\n" + date + "\n" + day);

is better right? When printing "day" I get 2. Are Mondays numbered 2 and sundays numbered 1 etc?

fedevapsa at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 7
RTFM, http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.htmlCalendar defines constants to check your return values against.
thomas.behra at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...
# 8
Calendar has a DAY_OF_WEEK_IN_MONTH setting. Setting a week number with this, together with setting DAY_OF_WEEK will get you the nth Monday (or Friday) in the month.Giving a negative value will get you the last, or last but one Monday/Friday in the month.
malcolmmca at 2007-7-12 9:24:35 > top of Java-index,Java Essentials,Java Programming...