Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting int value for day of week from Calendar class 2

Status
Not open for further replies.

ob1kanobee

Programmer
Dec 27, 2002
47
0
0
US
I have my Calendar date set for Wednesday, February 1, 2006.

I am trying to get the int value for the day of week for that date, which I assume should return 3 for a Wednesday.

Calendar myCal = Calendar.getInstance();
firstCal.set(Calendar.MONTH,2);
firstCal.set(Calendar.DAY_OF_MONTH,1);
firstCal.set(Calendar.YEAR,2006);

int firstDayOfWeek = firstCal.DAY_OF_WEEK;

It returns a 7. What am I doing wrong?
 
I'm confused.
The value I'm getting back now for variable firstCal is correct for Wednesday, February 1, 2006, but the firstCal.DAY_OF_WEEK is still returning a 7?
 
Here's an easy example that will result in day of week is 7:
Date thisDate = new Date("1-Feb-2006 10:35:00 PM");
Calendar firstCal = Calendar.getInstance();
firstCal.setTime(thisDate);
System.out.println("Date = " + firstCal.getTime());
int firstday = firstCal.DAY_OF_WEEK; // The first day (0-6) of the target month.


What I am really trying to do is take the code below which is a floating holiday method and replace everywhere I am using the Date function calls with the Calendar class.

Arguments are: current year, month number, week number, day number, event name
This should return me the day of the month, which works correctly using the Date method as shown below.

Code:
        Long dayofmonth = floatingHoliday(new Long(2006),new Long(1),new Long(3),new Long(2),"Martin Luther King's Birthday");

    public Long floatingHoliday(Long eventYear,Long eventMonth,Long weekNumber,Long dayOfWeek,String eventName) throws Exception {
        // Floating holidays/events of the events.js file uses:
        //      the Month field for the Month (here it becomes the targetmo field)
        //      the Day field as the Cardinal Occurrence  (here it becomes the cardinaloccurrence field)
        //              1=1st, 2=2nd, 3=3rd, 4=4th, 5=5th, 6=6th occurrence of the day listed next
        //      the Year field as the Day of the week the event/holiday falls on  (here it becomes the targetday field)
        //              1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thurday, 6=Friday, 7=Saturday
        //      example: "F",   "1",    "3",    "2", = Floating holiday in January on the 3rd Monday of that month.
        //
        // In our code below:
        //      targetyr is the active year
        //      targetmo is the active month (1-12)
        //      cardinaloccurrence is the xth occurrence of the targetday (1-6)
        //      targetday is the day of the week the floating holiday is on
        //              0=Sun; 1=Mon; 2=Tue; 3=Wed; 4=Thu; 5=Fri; 6=Sat
        //              Note: subtract 1 from the targetday field if the info comes from the events.js file
        //
        // Note:
        //      If Memorial Day falls on the 22nd, 23rd, or 24th, then we add 7 to the dayofmonth to the result.
        //
        // Example: targetyr = 2052; targetmo = 5; cardinaloccurrence = 4; targetday = 1
        //      This is the same as saying our floating holiday in the year 2052, is during May, on the 4th Monday
        //
        Long dayofmonth = new Long(0);     // zero out our TransitExceptions day variable.
        Long dayOffset = new Long(0);
        if(eventName.startsWith("Memorial") && eventMonth.equals(new Long("5")))
        { // Get last Monday of the month
            Long lastdayInMay = new Long("31");
            Date lastdate = new Date(eventMonth.toString()+"/"+ lastdayInMay.toString() +"/"+eventYear.toString());      // Object Storing the first day of the current month.
            int lastday = lastdate.getDay() + 1; // The first day (1-7 = Sun-Sat) of the target month.
            if(lastday == 2) // lastday is a Monday, leave as is...
            {
                dayofmonth = lastdayInMay;
            } else if(lastday == 1) { // lastday is a Sunday...
                dayofmonth = new Long(Math.round(lastdayInMay.longValue() - new Long("5").longValue()));
            } else if(lastday > 2) { // lastday is a Tue-Sat...
                dayOffset = new Long(Math.round(lastday - 2));
                dayofmonth = new Long(Math.round(lastdayInMay.longValue() - dayOffset.longValue()));
            }
        } else {
            Date firstdate = new Date(eventMonth.toString()+"/1/"+eventYear.toString());      // Object Storing the first day of the current month.
            int firstday = firstdate.getDay() + 1; // The first day (1-7) of the target month.

            if(dayOfWeek.intValue() >= firstday)
            {
                weekNumber = new Long(weekNumber.longValue() - 1);
            }
            dayofmonth = new Long(Math.round(Math.round(Math.round(dayOfWeek.intValue() - firstday)+1)+Math.round(weekNumber.longValue()*7)));
        }
        return dayofmonth;
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top