Japanese and calendar date conversion

Source: Internet
Author: User

During multi-country application development, the date conversion problem is often encountered in different regions. The conversion between Japanese and calendar is special.

The Japanese calendar marks each emperor's ruling period as an epoch. The current epoch is the Heisei epoch, which began in 1989 AD. The Epoch name is usually displayed before the year. For example, in July 2001 AD, the Japanese calendar is Heisei for 13 years. Note that the first year of the epoch is called "gannen". Therefore, the year 1989 AD is the year of the Japanese Calendar "Heisei gannen.

Epoch name Epoch abbreviation Calendar date
Heisei) Ping (H, H) January 8, 1989 to date
Showa) Zhao (S, S) December 25-19, 1926-January 7
Taisho) Large (t, t) July 30-19, 1912-December 24
Meiji (Meiji) Ming (M, m) September 8-19, 1868-July 29

Therefore, according to the habits of the Japanese calendar, 2006/09/26 should be expressed as a year from January 18, September 26 or H18/9/26.

In international applications, the cultureinfo class (under the system. Globalization namespace) is used to control the region where the date is displayed.

The japanesecalendar class (under the namespace system. Globalization) indicates the Japanese calendar.

The following code converts dates:

1) 2006/09/26 to 18/09/26

Cultureinfo CI = new cultureinfo ("Ja-JP ");

Calendar Cal = new japanesecalendar ();

CI. datetimeformat. Calendar = Cal;

Datetime dt = datetime. parse ("2006/09/26 ");

String strwareki = DT. tostring ("ggyy/MM/DD", CI );

Console. writeline (strwareki); // output: flat to 18/09/26

Of course, gyy/MM/dd can also use gyy-mm-dd.

2) convert to 18/09/26

Cultureinfo CI = new cultureinfo ("Ja-JP ");

Calendar Cal = new japanesecalendar ();

CI. datetimeformat. Calendar = Cal;

String strwareki = "flat to 18/09/26 ";

String strdate = datetime. parseexact (strwareki, "ggyy/MM/DD", CI). tostring ("yyyy/mm/DD ");

Console. writeline (strdate); // output: 2006/09/26

 

PS: strwareki = "flat 18/09/26"; the conversion can also be successful.

3) 2006/09/26 to flat 18/09/26 ("flat" is short for "flat)

Cultureinfo CI = new cultureinfo ("Ja-JP ");

Calendar Cal = new japanesecalendar ();

CI. datetimeformat. Calendar = Cal;

Datetime dt = datetime. parse ("2006/09/26 ");

String strwareki = CI. datetimeformat. getabbreviatederaname (Cal. ge1_( DT) + dt. tostring ("YY/MM/DD", CI );

Console. writeline (strwareki); // output: Flat 18/09/26

The getabbreviatederaname () method obtains the abbreviation of the epoch name based on the epoch number.

4) 2006/09/26 to H18/09/26 ("H" is short for "flat)

Here, because datetimeformat does not provide a direct method, it is a little troublesome.

Method 1:

String [] engeras = {"M", "T", "S", "H "};

Calendar Cal = new japanesecalendar ();

Datetime dt = datetime. parse ("2006/09/26 ");

String strwareki = DT. tostring ("{0} {1}/MM/DD ");

Strwareki = string. Format (strwareki, engeras [Cal. ge1_( DT)-1], Cal. getyear (DT). tostring ("00 "));

Console. writeline (strwareki); // output: H18/09/26

This method has poor scalability. In the event that the Japanese Emperor crashes today, the statements in this section are not correct...

Method 2:

Using Reflection, You need to reference system. Reflection to obtain non-public attributes of datetimeformatinfo-abbreviatedenglisheranames

Cultureinfo CI = new cultureinfo ("Ja-JP ");

Calendar Cal = new japanesecalendar ();

CI. datetimeformat. Calendar = Cal;

Datetime dt = datetime. parse ("2006/09/26 ");

Type T = typeof (datetimeformatinfo );

Propertyinfo Pi = T. getproperty ("abbreviatedenglisheranames", bindingflags. nonpublic | bindingflags. instance );

String [] engeras = (string []) PI. getvalue (CI. datetimeformat, null );

Int era = Cal. ge1_( DT );

String strwareki = engeras [era-1] + dt. tostring ("YY/MM/DD", CI );

Console. writeline (strwareki); // output: H18/09/26

This method can be used to expand the application by relying on system. dll, which is a good conversion method.

The above are some commonly used Japanese calendar date conversion code. As long as you know how to use the cultureinfo and calendar objects, the date display in other regions is also unavailable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.