西暦=>和暦、和暦=>西暦

#region 西暦=>和暦、和暦=>西暦
        /// <summary>
        /// 西暦=>和暦
        /// </summary>
        /// <param name="strDate">西暦日期</param>
        /// <param name="strWarekiFormat">例:ggyy年DD月dd日</param>
        /// <returns>例:平成23年08月03日 </returns>
        public string ConvertDateToWareki01(string strDate, string strWarekiFormat)
        {
             string strWareki = "";
             try
             {
                 CultureInfo ci = new CultureInfo("ja-JP", true);
                 Calendar cal = new JapaneseCalendar();
                 ci.DateTimeFormat.Calendar = cal;
                 DateTime vCurrentDate = DateTime.Parse(strDate);

                 strWareki = vCurrentDate.ToString(strWarekiFormat, ci.DateTimeFormat);
             }
             catch
             {
             }
            return strWareki;
        }
        /// <summary>
        /// 西暦=>和暦
        /// </summary>
        /// <param name="strDate">西暦日期</param>
        /// <param name="strWarekiFormat">例:yy年DD月dd日</param>
        /// <returns>例:平23年08月03日</returns>
        public string ConvertDateToWareki02(string strDate, string strWarekiFormat)
        {
            string strWareki = "";
            try
            {
                CultureInfo ci = new CultureInfo("ja-JP", true);
                Calendar cal = new JapaneseCalendar();
                ci.DateTimeFormat.Calendar = cal;
                DateTime dt = DateTime.Parse(strDate);

                strWareki = ci.DateTimeFormat.GetAbbreviatedEraName(cal.GetEra(dt)) + dt.ToString(strWarekiFormat, ci);
            }
            catch
            {
            }
            return strWareki;
        }
        /// <summary>
        /// 西暦=>和暦
        /// </summary>
        /// <param name="strDate">西暦日期</param>
        /// <param name="strWarekiFormat">例:yy年DD月dd日</param>
        /// <returns>例:H23年08月03日</returns>
        public string ConvertDateToWareki03(string strDate, string strWarekiFormat)
        {
            string strWareki = "";
            try
            {
                string[] engEras = { "M", "T", "S", "H" };
                CultureInfo ci = new CultureInfo("ja-JP", true);
                Calendar cal = new JapaneseCalendar();
                ci.DateTimeFormat.Calendar = cal;
                DateTime dt = DateTime.Parse(strDate);

                strWareki = engEras[cal.GetEra(dt) - 1] + dt.ToString(strWarekiFormat, ci);
            }
            catch
            {
            }
            return strWareki;
        }
        /// <summary>
        /// 西暦=>和暦
        /// </summary>
        /// <param name="strDate">西暦日期</param>
        /// <param name="strWarekiFormat">例:yy年DD月dd日</param>
        /// <returns>例:H23年08月03日</returns>
        public string ConvertDateToWareki04(string strDate, string strWarekiFormat)
        {
            string strWareki = "";
            try
            {
                CultureInfo ci = new CultureInfo("ja-JP", true);
                Calendar cal = new JapaneseCalendar();
                ci.DateTimeFormat.Calendar = cal;
                DateTime dt = DateTime.Parse(strDate);

                Type t = typeof(DateTimeFormatInfo);
                PropertyInfo pi = t.GetProperty("AbbreviatedEnglishEraNames", BindingFlags.NonPublic | BindingFlags.Instance);
                string[] engEras = (string[])pi.GetValue(ci.DateTimeFormat, null);

                strWareki = engEras[cal.GetEra(dt) - 1] + dt.ToString(strWarekiFormat, ci);
            }
            catch
            {
            }
            return strWareki;
        }
        /// <summary>
        /// 和暦=>西暦
        /// </summary>
        /// <param name="strWareki">和暦日期(例:平成23年08月03日、平23年08月03日、H23年08月03日)</param>
        /// <param name="strWarekiFormat">和暦日期と対応フォマット(例:yy年MM月dd日)</param>
        /// <param name="iStartIndex">元号の次位置</param>
        /// <param name="strDateFormat">例:yyyy/MM/dd</param>
        /// <returns>例:2011/08/03</returns>
        public string ConvertWarekiToDate(string strWareki, string strWarekiFormat, string strDateFormat,int iStartIndex)
        {
            string strDate = "";
            try
            {
                CultureInfo ci = new CultureInfo("ja-JP", true);
                Calendar cal = new JapaneseCalendar();
                ci.DateTimeFormat.Calendar = cal;
                strDate = DateTime.ParseExact(strWareki.Substring(iStartIndex, strWareki.Length - iStartIndex), strWarekiFormat, ci).ToString(strDateFormat);
            }
            catch
            {
            }
            return strDate;
        }
        #endregion

posted on 2011-08-03 10:59  念念啊  阅读(1375)  评论(0编辑  收藏  举报