Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

GregorianCalendar Class

Represents the Gregorian calendar.

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class GregorianCalendar : Calendar
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class GregorianCalendar extends Calendar
SerializableAttribute 
ComVisibleAttribute(true) 
public class GregorianCalendar extends Calendar

The Gregorian calendar recognizes two eras: B.C. (before Christ) or B.C.E. (before common era), and A.D. (Latin "Anno Domini", which means "in the year of the Lord") or C.E. (common era). This implementation of the GregorianCalendar class recognizes only the current era (A.D. or C.E.).

A leap year in the Gregorian calendar is defined as a year that is evenly divisible by four, except if it is divisible by 100; however, years that are divisible by 400 are leap years. For example, the year 1900 was not a leap year, but the year 2000 was. A common year has 365 days and a leap year has 366 days.

The Gregorian calendar has 12 months with 28 to 31 days each: January (31 days), February (28 or 29 days), March (31 days), April (30 days), May (31 days), June (30 days), July (31 days), August (31 days), September (30 days), October (31 days), November (30 days), and December (31 days). February has 29 days during leap years and 28 during common years.

Each CultureInfo supports a set of calendars. The Calendar property returns the default calendar for the culture, and the OptionalCalendars property returns an array containing all the calendars supported by the culture. To change the calendar used by a CultureInfo, set the Calendar property of CultureInfo.DateTimeFormat to a new Calendar.

GetEra ignores punctuation in abbreviated era names, only if the GregorianCalendar is selected in DateTimeFormatInfo.Calendar and the culture uses "A.D." as the era name; that is, "A.D." is equivalent to "AD".

The following code example shows that DateTimeFormatInfo ignores the punctuation in the era name, only if the calendar is Gregorian and the culture uses the era name "A.D.".

using System;
using System.Globalization;


public class SamplesGregorianCalendar  {

   public static void Main()  {

      // Creates strings with punctuation and without.
      String strADPunc = "A.D.";
      String strADNoPunc = "AD";
      String strCEPunc = "C.E.";
      String strCENoPunc = "CE";

      // Calls DTFI.GetEra for each culture that uses GregorianCalendar as the default calendar.
      Console.WriteLine( "            ----- AD -----  ----- CE -----" );
      Console.WriteLine( "CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR" );
      foreach ( CultureInfo myCI in CultureInfo.GetCultures( CultureTypes.SpecificCultures ) )  {
         Console.Write( "{0,-12}", myCI );
         Console.Write( "{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra( strADPunc ), myCI.DateTimeFormat.GetEra( strADNoPunc ) );
         Console.Write( "{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra( strCEPunc ), myCI.DateTimeFormat.GetEra( strCENoPunc ) );
         Console.Write( "{0}", myCI.Calendar );
         Console.WriteLine();
      }

   }

}

/*
This code produces the following output.  This output has been cropped for brevity.

            ----- AD -----  ----- CE -----
CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-HK       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

*/

import System.* ;
import System.Globalization.* ;

public class SamplesGregorianCalendar
{
    public static void main(String[] args)
    {
        // Creates strings with punctuation and without.
        String strADPunc = "A.D.";
        String strADNoPunc = "AD";
        String strCEPunc = "C.E.";
        String strCENoPunc = "CE";

        // Calls DTFI.GetEra for each culture that uses GregorianCalendar as 
        //the default calendar.
        Console.WriteLine("            ----- AD -----  ----- CE -----");
        Console.WriteLine("CULTURE PUNC NO PUNC  PUNC   NO PUNC  CALENDAR");
        for (int iCtr = 0; iCtr < (CultureInfo.GetCultures(
            CultureTypes.SpecificCultures).length); iCtr++) {

            CultureInfo myCI = CultureInfo.GetCultures( 
                CultureTypes.SpecificCultures)[iCtr];
            Console.Write("{0,-12}", myCI);
            Console.Write("{0,-7}{1,-9}", System.Convert.ToString(
                myCI.get_DateTimeFormat().GetEra(strADPunc)),System.Convert.
                ToString(myCI.get_DateTimeFormat().GetEra(strADNoPunc)));
            
            Console.Write("{0,-7}{1,-9}", System.Convert.ToString(
                myCI.get_DateTimeFormat().GetEra(strCEPunc)), 
                System.Convert.ToString(myCI.get_DateTimeFormat().
                GetEra(strCENoPunc)));
            
            Console.Write("{0}", myCI.get_Calendar());
            Console.WriteLine();
        }
    } //main
} //SamplesGregorianCalendar

/*
This code produces the following output.  This output has been cropped 
for brevity.

            ----- AD -----  ----- CE -----
CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
ar-IQ       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-EG       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-LY       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-DZ       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-MA       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-TN       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-OM       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-YE       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-SY       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-JO       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-LB       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-KW       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-AE       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-BH       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ar-QA       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
bg-BG       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
ca-ES       -1     -1       -1     -1       System.Globalization.
                                            GregorianCalendar
zh-TW       -1     -1       -1     -1       System.Globalization.
                                            GregorianCalendar
zh-CN       -1     -1       -1     -1       System.Globalization.
                                            GregorianCalendar
zh-HK       -1     -1       -1     -1       System.Globalization.
                                            GregorianCalendar
zh-SG       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
zh-MO       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
cs-CZ       -1     -1       -1     -1       System.Globalization.
                                            GregorianCalendar
da-DK       1      1        -1     -1       System.Globalization.
                                            GregorianCalendar
*/

System.Object
   System.Globalization.Calendar
    System.Globalization.GregorianCalendar
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.