日期处理(转)

private void button1_Click(object sender, EventArgs e)
{
               DateTime nDate = Convert.ToDateTime(dateTimePicker1.Text); //当前需要转换的日期
               string jrDate = ChinaDate.GetChinaHoliday(nDate); //获取农历节日
               string jrMonth = ChinaDate.GetMonth(nDate); //获取农历月份
               string jrDay = ChinaDate.GetDay(nDate); //获取农历日期
               DateTime jrSDate = ChinaDate.GetSunYearDate(nDate); //阳历转阴历
               string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
               string week = weekdays[Convert.ToInt32(nDate.DayOfWeek)];
               this.textBox1.Text = jrSDate.ToString();
        }

        /// <summary>
        /// 农历与阴历之间相互转化工具类
        /// </summary>
        public class ChinaDate
        {
            #region 农历信息获取
            private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar();
            private static Hashtable gHoliday = new Hashtable();
            private static Hashtable nHoliday = new Hashtable();
            private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
            private static int[] JQData = { 0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };

            static ChinaDate(){
                //公历节日
                gHoliday.Add("0101", "元旦");
                gHoliday.Add("0214", "情人节");
                gHoliday.Add("0305", "雷锋日");
                gHoliday.Add("0308", "妇女节");
                gHoliday.Add("0312", "植树节");
                gHoliday.Add("0315", "消费者权益日");
                gHoliday.Add("0401", "愚人节");
                gHoliday.Add("0501", "劳动节");
                gHoliday.Add("0504", "青年节");
                gHoliday.Add("0601", "儿童节");
                gHoliday.Add("0701", "建党节");
                gHoliday.Add("0801", "建军节");
                gHoliday.Add("0910", "教师节");
                gHoliday.Add("1001", "国庆节");
                gHoliday.Add("1224", "平安夜");
                gHoliday.Add("1225", "圣诞节");
                //农历节日
                nHoliday.Add("0101", "春节");
                nHoliday.Add("0115", "元宵节");
                nHoliday.Add("0505", "端午节");
                nHoliday.Add("0815", "中秋节");
                nHoliday.Add("0909", "重阳节");
                nHoliday.Add("1208", "腊八节");
            }

            /// <summary>
            /// 获取农历
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetChinaDate(DateTime dt){
                if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime){
                    //日期范围:1901 年 2 月 19 日 - 2101 年 1 月 28 日
                    throw new Exception(string.Format("日期超出范围!必须在{0}到{1}之间!", china.MinSupportedDateTime.ToString("yyyy-MM-dd"), china.MaxSupportedDateTime.ToString("yyyy-MM-dd")));
                }
                string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt));
                string strJQ = GetSolarTerm(dt);
                if (strJQ != ""){
                    str += " (" + strJQ + ")";
                }
                string strHoliday = GetHoliday(dt);
                if (strHoliday != ""){
                    str += " " + strHoliday;

                }
                string strChinaHoliday = GetChinaHoliday(dt);
                if (strChinaHoliday != ""){
                    str += " " + strChinaHoliday;
                }
                return str;
            }

            /// <summary>
            /// 获取农历年份
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetYear(DateTime dt)
            {
                int yearIndex = china.GetSexagenaryYear(dt);
                string yearTG = " 甲乙丙丁戊己庚辛壬癸";
                string yearDZ = " 子丑寅卯辰巳午未申酉戌亥";
                string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪";
                int year = china.GetYear(dt);
                int yTG = china.GetCelestialStem(yearIndex);
                int yDZ = china.GetTerrestrialBranch(yearIndex);
                string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]);
                return str;
            }

            /// <summary>
            /// 获取农历月份
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetMonth(DateTime dt){
                int year = china.GetYear(dt);
                int iMonth = china.GetMonth(dt);
                int leapMonth = china.GetLeapMonth(year);
                bool isLeapMonth = iMonth == leapMonth;
                if (leapMonth != 0 && iMonth >= leapMonth){
                    iMonth--;
                }
                string szText = "正二三四五六七八九十";
                string strMonth = isLeapMonth ? "闰" : "";
                if (iMonth <= 10){
                    strMonth += szText.Substring(iMonth - 1, 1);
                }
                else if (iMonth == 11){
                    strMonth += "十一";
                }
                else{
                    strMonth += "腊";
                }
                return strMonth + "月";
            }

            /// <summary>
            /// 获取农历日期
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetDay(DateTime dt){
                int iDay = china.GetDayOfMonth(dt);
                string szText1 = "初十廿三";
                string szText2 = "一二三四五六七八九十";
                string strDay;
                if (iDay == 20){
                    strDay = "二十";
                }
                else if (iDay == 30){
                    strDay = "三十";
                }
                else{
                   strDay = szText1.Substring((iDay - 1) / 10, 1);
                   strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
                }
                return strDay;
            }

            /// <summary>
            /// 获取节气
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetSolarTerm(DateTime dt){
                DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0);
                DateTime dtNew;
                double num;
                int y;
                string strReturn = "";
                y = dt.Year;
                for (int i = 1; i <= 24; i++){
                    num = 525948.76 * (y - 1900) + JQData[i - 1];
                    dtNew = dtBase.AddMinutes(num);
                    if (dtNew.DayOfYear == dt.DayOfYear){
                        strReturn = JQ[i - 1];
                    }
                }
                return strReturn;
            }

            /// <summary>
            /// 获取公历节日
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetHoliday(DateTime dt){
                string strReturn = "";
                object g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")];
                if (g != null){
                    strReturn = g.ToString();
                }
                return strReturn;
            }

            /// <summary>
            /// 获取农历节日
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string GetChinaHoliday(DateTime dt){
                string strReturn = "";
                int year = china.GetYear(dt);
                int iMonth = china.GetMonth(dt);
                int leapMonth = china.GetLeapMonth(year);
                int iDay = china.GetDayOfMonth(dt);
                if (china.GetDayOfYear(dt) == china.GetDaysInYear(year)){
                    strReturn = "除夕";
                }
                else if (leapMonth != iMonth){
                    if (leapMonth != 0 && iMonth >= leapMonth){
                        iMonth--;
                    }
                    object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")];
                    if (n != null){
                        if (strReturn == ""){
                            strReturn = n.ToString();
                        }
                        else{
                            strReturn += " " + n.ToString();
                        }
                    }
                }
                return strReturn;
            }
            #endregion

            #region 阴历-阳历-转换

            /// <summary>
            /// 阴历转为阳历
            /// </summary>
            /// <param name="year">指定的年份</param>
            public static DateTime GetLunarYearDate(DateTime dt){
                int cnYear = china.GetYear(dt);
                int cnMonth = china.GetMonth(dt);
                int num1 = 0;
                int num2 = china.IsLeapYear(cnYear) ? 13 : 12;
                while (num2 >= cnMonth){
                    num1 += china.GetDaysInMonth(cnYear, num2--);
                }
                num1 = num1 - china.GetDayOfMonth(dt) + 1;
                return dt.AddDays(num1);
            }

            /// <summary>
            /// 阳历转为阴历
            /// </summary>
            /// <param name="dt">公历日期</param>
            /// <returns>农历的日期</returns>
            public static DateTime GetSunYearDate(DateTime dt){
                int year = china.GetYear(dt);
                int iMonth = china.GetMonth(dt);
                int iDay = china.GetDayOfMonth(dt);
                int leapMonth = china.GetLeapMonth(year);
                bool isLeapMonth = iMonth == leapMonth;
                if (leapMonth != 0 && iMonth >= leapMonth){
                    iMonth--;
                }
                string str = string.Format("{0}-{1}-{2}", year, iMonth, iDay);
                DateTime dtNew = DateTime.Now;
                try{
                    dtNew = Convert.ToDateTime(str); //防止出现2月份时,会出现超过时间,出现“2015-02-30”这种错误日期
                }
                catch
                {}
                return dtNew;
            }
            #endregion
        }
时间: 2024-10-22 20:07:34

日期处理(转)的相关文章

plsql存储过程日期类型和天的互转

floor(to_number(tt.FDATE-to_date('0001-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss'))) 上面这句能将型如'2017-09-20 00:00:00' 的日期转为自'0001-01-01 00:00:00'以来的天数. 其它相关的需求可以参考: http://www.cnblogs.com/hanyun/archive/2012/04/28/2475642.html

8.03 确定两个日期之间的工作日数目

问题:给定两个日期,求它们之间(包括这两个日期本身)有多少个"工作"日.select sum(case             when date_format(             date_add(jones_hd,interval t500.id - 1, 'DY'),'%a')             in ('SAT', 'SUN')              then 0 else 1           end) as days  from (select max(c

JavaSE8基础 File lastModified 获取文件夹的修改日期

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 通过这张截图可以看到 测试文件夹 的修改日期. code: package jizuiku0; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; /* * @version V17.09 */ pu

SpringMVC后台使用对象接受参数字符串转日期

在springMVC配置文件中加入: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property

常见的日期处理方式总结

■ 目录 日期对象 日期的取得与设定 转换为字符串 转换为数值 ■ 一览 Date(), getDate(), getDay(), getFullYear(), getHours(), getMilliseconds(), getMinutes(), getMonth(), getSeconds(), getTime(), getTimezoneOffset(), getUTCDate(),getUTCDay(), getUTCFullYear(), getUTCHours(), getUTCM

python selenium 处理时间日期控件(十五)

测试过程中经常遇到时间控件,需要我们来选择日期,一般处理时间控件通过层级定位来操作或者通过调用js来实现. 1.首先我们看一下如何通过层级定位来操作时间控件. 通过示例图可以看到,日期控件是无法输入日期,点击后弹出日期列表供我们选择日期,自己找了一个日期控制演示一下,通过两次定位,选择了日期 #-*- coding:utf-8 -*- import time from selenium import webdriver driver = webdriver.Chrome() driver.get

js日期

<title>范例2-6</title> <script language="javascript"> <!-- var dateObj = new Date(); // 创建一个日期对象 dateObj.setYear( 2007 ); // 设置日期对象的年份 dateObj.setDate( 20 ); // 设置日期对象的日期 dateObj.setMonth( 4 ); // 设置日期对象的月份 // 显示日期对象中的时间 alert

Java日期时间(Date/Time)

获取当前日期和时间 在Java中容易得到当前的日期和时间.可以使用一个简单的Date对象的toString()方法,如下所示打印当前日期和时间: import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date using toStr

每天一个JS 小demo之日历制作。主要知识点:日期函数和对于函数封装的灵活运用

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> td { text-align: center; } </style></head> <body> <p> <select id="yearS

顺序结构显示日期与时间

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace 顺序结构时间日期{ public partial class Form1 : Form { public Form1() {