栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Unity农历和公历相互转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity农历和公历相互转换

目录
  • 前言
  • 农历转公历
  • 公历转农历


前言

Unity农历和公历相互转换,使用 System.Globalization.ChineseLunisolarCalendar 实现,但是需要注意的是在.NET2.0是无法调用GetMonth等方法的,在.NET2.0中,方法被标记为[MonoTODO]。


农历转公历
    /// 
    /// 农历转公历
    /// 需.net4.6可用
    /// 
    /// 2021-07-24
    /// 2021-08-31
    public static string LunarToSolar( string time)
    {
        System.Globalization.ChineseLunisolarCalendar cncld = new System.Globalization.ChineseLunisolarCalendar();
        int year = Convert.ToInt32(time.Split('-')[0]);
        int month = Convert.ToInt32(time.Split('-')[1]);
        int day = Convert.ToInt32(time.Split('-')[2]);
        int flag = cncld.GetLeapMonth(year);
        DateTime dtnl = cncld.ToDateTime(year, month, day, 0, 0, 0, 0);
        dtnl = flag > 0 ? dtnl.AddMonths(1) : dtnl;
        return dtnl.ToString("yyyy-MM-dd");
    }

公历转农历
    /// 
    /// 公历转农历
    /// 需.net4.6可用
    /// 
    /// 2021-08-31
    /// 2021-07-24
    public static string SolarToLunar( string time)
    {
        System.Globalization.ChineseLunisolarCalendar cncld = new System.Globalization.ChineseLunisolarCalendar();
        DateTime dt = DateTime.Parse(time);
        int year = cncld.GetYear(dt);
        int flag = cncld.GetLeapMonth(year);
        int month = flag > 0 ? cncld.GetMonth(dt) - 1 : cncld.GetMonth(dt);
        int day = cncld.GetDayOfMonth(dt);
        return $"{year}-{(month.ToString().Length == 1 ? "0" + month : month + "")}-{(day.ToString().Length == 1 ? "0" + day : day + "")}";
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014235.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号