harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ramana Polavarapu (JIRA)" <j...@apache.org>
Subject [jira] Commented: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
Date Thu, 05 Nov 2009 08:19:32 GMT

    [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773827#action_12773827
] 

Ramana Polavarapu commented on HARMONY-4907:
--------------------------------------------

Hi Tony:

I looked into this bug.  The problem is with GregorianCalendar.fullFieldCalk method.  Specifically,
the culprit is line 551:
fields[WEEK_OF_MONTH] = (date - 1 + mod7(days - date - 2
                - (getFirstDayOfWeek() - 1))) / 7 + 1;
        This computation does not take into consideration minimalDaysInFirstWeek.  We have
to modify above lines with the following:
        fields[WEEK_OF_MONTH] = computeWeekOfMonth();
To compute week of month, any one of the following two methods can be used.  I wrote the first
one.  Later, I extracted from your test case the second method.  There are two other points
that I would like to make:
1. In your test case, you need to add the following line:
c.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);
2. When this variable is not set, RI returns 1 but Harmony returns 4.  Do we need to match
the behavior of RI? 

	/**
	 * The algorithm first figures out the beginning of the next week.
	 * The for loop in the method accomplishes the above task.
	 * Then, it computes the first occurrence
	 * of the first day of the week in the given month.
	 * Before the first occurrence if there are more days than
	 * the minimum required days for first week,
	 * those days belong to week 1.
	 * Otherwise, they belong to week 0.
	 * This method should not be called before 
	 * <code>filds[DATE] is set.
	 * @return week of the month.  The value will be 0 if 
	 * minimum required days for a week are more than the days before 
	 * the first "first day of week" at the beginning of the month.
	 */
    private int computeWeekOfMonth() {
    	int firstDay = getFirstDayOfWeek();
    	int tempDate = fields[DATE];
    	int tempDayOfWeek = DAY_OF_WEEK;
    	for(int i = 0; i < 7; i++)
    		if(firstDay == tempDayOfWeek)
    			break;
    		else {
    			tempDate++;
    			tempDayOfWeek++;
    		}
    	int tempFirstWeek = tempDate%7 >= getMinimalDaysInFirstWeek() ? 1 : 0;
    	return fields[DATE] / 7 + tempFirstWeek;
    }

    /**
     * This method does the same as above.
     * This method more compact.
     * Consequently, following logic is a bit more difficult.
     * 
     * @return
     */
    private int computeWeekOfMonthTony() {
		int dayOfWeek = fields[DAY_OF_WEEK];
		int relativeDayOfWeek = (7 + dayOfWeek - getFirstDayOfWeek()) % 7;
		int relativeDayOfFirst = (relativeDayOfWeek - fields[DATE] + 1 + 35) % 7;
		int weekOfFirst = ((7 - relativeDayOfFirst) >= getMinimalDaysInFirstWeek()) ? 1 : 0;
		return (fields[DATE] + relativeDayOfFirst - 1) / 7 + weekOfFirst;
	}

Regards,

Ramana
	


> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>            Assignee: Tony Wu
>            Priority: Trivial
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message