• 日期对象处理


    thisMonth()    //返回月初

    thisMonthEnd()    //返回月末

    thisSeason()    //返回季度初

    thisSeasonEnd()    //返回季度末

    thisYear()    //返回年初

    thisYearEnd()    //返回年末

    public String thisMonth(DateTime date)
            {
                String strY = null;
                int x, y;
                x = Convert.ToInt32(date.Year.ToString());
                y = Convert.ToInt32(date.Month.ToString());
                strY = y >= 10 ? y.ToString() : ("0" + y); return x + "年" + strY + "月01日";
            }

            public String thisMonthEnd(DateTime date)
            {
                int x, y;
                String strY = null;
                String strZ = null;
                bool leap = false;
                x = Convert.ToInt32(date.Year.ToString());
                y = Convert.ToInt32(date.Month.ToString());
                if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
                {
                    strZ = "31";
                }
                if (y == 4 || y == 6 || y == 9 || y == 11)
                {
                    strZ = "30";
                }
                if (y == 2)
                {
                    leap = leapYear(x);
                    if (leap)
                    {
                        strZ = "29";
                    }
                    else
                    {
                        strZ = "28";
                    }
                }
                strY = y >= 10 ? y.ToString() : ("0" + y);
                return x + "年" + strY + "月" + strZ + "日";
            }

            public String thisSeason(DateTime date)
            {
                String dateString = "";
                int x, y;
                x = Convert.ToInt32(date.Year.ToString());
                y = Convert.ToInt32(date.Month.ToString());
                if (y >= 1 && y <= 3)
                {
                    dateString = x + "-" + "01" + "-" + "01";
                }
                if (y >= 4 && y <= 6)
                {
                    dateString = x + "-" + "04" + "-" + "01";
                }
                if (y >= 7 && y <= 9)
                {
                    dateString = x + "-" + "07" + "-" + "01";
                }
                if (y >= 10 && y <= 12)
                {
                    dateString = x + "-" + "10" + "-" + "01";
                }
                return dateString;
            }

            public String thisSeasonEnd(DateTime date)
            {
                String dateString = "";
                int x, y;
                x = Convert.ToInt32(date.Year.ToString());
                y = Convert.ToInt32(date.Month.ToString());
                if (y >= 1 && y <= 3)
                {
                    dateString = x + "-" + "03" + "-" + "31";
                }
                if (y >= 4 && y <= 6)
                {
                    dateString = x + "-" + "06" + "-" + "30";
                }
                if (y >= 7 && y <= 9)
                {
                    dateString = x + "-" + "09" + "-" + "30";
                }
                if (y >= 10 && y <= 12)
                {
                    dateString = x + "-" + "12" + "-" + "31";
                }
                return dateString;
            }

            public String thisYear(DateTime date)
            {
                int x;
                x = Convert.ToInt32(date.Year.ToString());
                return x + "-01" + "-01";
            }

            public String thisYearEnd(DateTime date)
            {
                int x;
                x = Convert.ToInt32(date.Year.ToString());
                return x + "-12" + "-31";
            }

            public bool leapYear(int year)
            {
                bool leap;
                if (year % 4 == 0)
                {
                    if (year % 100 == 0)
                    {
                        if (year % 400 == 0) leap = true;
                        else leap = false;
                    }
                    else leap = true;
                }
                else leap = false;
                return leap;
            }

    作者:返回主页 linux运维-loring
    出处:http://www.cnblogs.com/zlf344242525/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    基本计算器 II
    查看JVM使用的什么垃圾收集器
    java nio 例子
    获取jvm加载的类
    对上传的二进制视频文件进行第一帧截取
    conda与pip
    微信聊天记录导出与分析
    k8s creationTimestamp 参数
    adb logcat使用及Debug技巧
    聊聊HDR
  • 原文地址:https://www.cnblogs.com/zlf344242525/p/2254870.html
Copyright © 2020-2023  润新知