BUG: The JapaneseCalendar.GetWeekOfYear method returns an incorrect value for past eras before January 8, 1989 (834493)
The information in this article applies to:
- Microsoft .NET Framework 1.0
- Microsoft .NET Framework 1.1
SYMPTOMSWhen you use the JapaneseCalendar.GetWeekOfYear method to obtain the week of the year that includes the date in the
specified DateTime object,
the JapaneseCalendar.GetWeekOfYear method
may return an incorrect value.
This problem occurs only for past
eras. It does not occur for the current era, Heisei. According to the Gregorian
calendar, the current era began on January 8, 1989. CAUSEThis problem occurs because the JapaneseCalendar.GetWeekOfYear method internally uses the following overload of the JapaneseCalendar.ToDateTime method when days are calculated: public virtual DateTime ToDateTime(
int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond
);
However, by default, this overload of the JapaneseCalendar.ToDateTime method ignores the era information and returns a DateTime object with the current era. This behavior causes the JapaneseCalendar.GetWeekOfYear method to return an incorrect value if the specified date is in
past eras, that is, before the Gregorian calendar date of January 8,
1989. STATUSMicrosoft
has confirmed that this is a problem in the Microsoft products that are listed
in the "Applies to" section of this article.WORKAROUNDTo work around
this problem, use either one of the following methods:
- Use the GregorianCalendar.GetWeekOfYear method to obtain the week of the year. Because the GregorianCalendar class is not Japanese localized, use this method if you just want to obtain general calendar information.
- Subclass the JapaneseCalendar class, and override the JapaneseCalendar.ToDateTime method:
public override System.DateTime ToDateTime(
int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond)
{
//m_Era is a private Int32 variable that used to keep the era information of the specific date
return (ToDateTime(year, month, day, hour, minute, second, millisecond,m_Era));
}
Following is the complete code:[Serializable]
public class MyJapaneseCalendar: JapaneseCalendar
{
Int32 m_Era=0;
public override int GetWeekOfYear(
DateTime time,
CalendarWeekRule rule,
DayOfWeek firstDayOfWeek)
{
//Store the era information of the specific date to m_Era variable,
//which will be used in the following ToDateTime method:
m_Era=GetEra(time);
return base.GetWeekOfYear(time,rule,firstDayOfWeek);
}
public override System.DateTime ToDateTime(
int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond)
{
//m_Era is a private Int32 variable that used to keep
//the era information of the specific date
return (ToDateTime(year, month, day, hour, minute, second, millisecond,m_Era));
}
}
Modification Type: | Minor | Last Reviewed: | 2/26/2004 |
---|
Keywords: | kbcode kbBug KB834493 kbAudDeveloper |
---|
|