Microsoft KB Archive/834493

= BUG: The JapaneseCalendar.GetWeekOfYear method returns an incorrect value for past eras before January 8, 1989 =

Article ID: 834493

Article Last Modified on 5/17/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



SYMPTOMS
When 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.



CAUSE
This 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.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.



WORKAROUND
To 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)); } } 



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET, version 2002 or version 2003. On the File menu, point to New, and then click Project. The New Project dialog box appear. Under Project Types, click Visual C# Projects, and then select Console Application (.NET) under Templates.</li> In the Name box, type Q834493, and then click OK.</li>  Replace the code in the Class1.cs file with the following code: using System; using System.Globalization;

namespace Q834493 {   class Class1 {       public static void Main {           DateTime dt = new DateTime(1989, 1,7); JapaneseCalendar jc = new JapaneseCalendar; Console.WriteLine(&quot;-Output of JapaneseCalendar---&quot;); DisplayValue(jc,dt);

Console.WriteLine(&quot;-Output of GregorianCalendar---&quot;); GregorianCalendar jc1 = new GregorianCalendar; DisplayValue(jc1,dt); }

static void DisplayValue(Calendar cal, DateTime dt) {           Console.WriteLine(&quot;Date: &quot; + dt.ToString); Console.WriteLine(&quot;Day: &quot; + cal.GetDayOfWeek(dt)); Console.WriteLine(&quot;Week: &quot; + cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)); }   } } </li> On the File menu, click Save All to save all files.</li>  On the Debug menu, click Start Without Debugging to start the application.

The following output appears and displays different week information for the calendars: -Output of JapaneseCalendar--- Date: 1/7/1989 0:00:00 Day: Saturday Week: 2 -Output of GregorianCalendar--- Date: 1/7/1989 0:00:00 Day: Saturday Week: 1 Press any key to continue As you can see from this output, the week that is returned from the JapaneseCalendar.GetWeekOfYear method is incorrect. </li></ol>

<div class="references_section">

For more information about the Japanese calendar, visit the following Web site:http://msdn2.microsoft.com/en-us/library/system.globalization.japanesecalendar(vs.71).aspx

Additional query words: JapaneseCalendar GetWeekOfYear era Heisei

Keywords: kbcode kbbug KB834493

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.