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

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 "Applies to" 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));
    	}
    }

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Microsoft Visual Studio .NET, version 2002 or version 2003.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appear.
  3. Under Project Types, click Visual C# Projects, and then select Console Application (.NET) under Templates.
  4. In the Name box, type Q834493, and then click OK.
  5. 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("-----Output of JapaneseCalendar-------");
    			DisplayValue(jc,dt);
    
    			Console.WriteLine("-----Output of GregorianCalendar-------");
    			GregorianCalendar jc1 = new GregorianCalendar();
    			DisplayValue(jc1,dt);
    		}
    
    		static void DisplayValue(Calendar cal, DateTime dt)
    		{
    			Console.WriteLine("Date: " + dt.ToString());
    			Console.WriteLine("Day:  " + cal.GetDayOfWeek(dt));
    			Console.WriteLine("Week: " + cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay  , DayOfWeek.Sunday));
    		}
    	}
    }
    
  6. On the File menu, click Save All to save all files.
  7. 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.
For more information about the Japanese calendar, visit the following Web site:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationJapaneseCalendarClassTopic.asp

Modification Type:MinorLast Reviewed:2/26/2004
Keywords:kbcode kbBug KB834493 kbAudDeveloper