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

Java简单封装一个日历

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

Java简单封装一个日历

public class CalenderDate {
    //对象中的年份属性
    private int year;
    //对象中的月份属性
    private int mouth;
    //对象中的天属性
    private int day;

    
    public CalenderDate(int year, int mouth, int day) {
        this.year = year;
        this.mouth = mouth;
        this.day = day;
    }

    
    public int getweek() {
        int M = this.mouth >= 3 ? this.mouth - 2 : this.mouth + 10;
        int D = this.day;
        int year = this.mouth >= 3 ? this.year : this.year - 1;
        int Y = year % 100;
        int C = year / 100;
        int F = (26 * M - 2) / 10 + D + Y + Y / 4 + C / 4 - 2 * C;
        int W = F % 7;
        return W < 0 ? W + 7 : W;
    }

    
    public boolean before(CalenderDate date) {
        if (this.year > date.year) return false;
        if (this.year < date.year) return true;
        //然后比较月份
        if (this.mouth > date.mouth) return false;
        if (this.mouth < date.mouth) return true;
        //最后比就天数
        return this.day < date.day;

    }

    
    public boolean after(CalenderDate date) {
        if (this.year > date.year) return true;
        if (this.year < date.year) return false;
        //然后比较月份
        if (this.mouth > date.mouth) return true;
        if (this.mouth < date.mouth) return false;
        //最后比就天数
        return this.day > date.day;
    }
    public int maxDay(int month){
        return switch (month){
            case 1,3,5,7,8,10,12 -> 31;
            case 2 -> feb();
            default -> 30;
        };

    }

    
    public int getDate() {
        int day = this.day;
        for (int i = 1;i  

然后我们简单的测试一下

public class Testclass {
    public static void main(String[] args) {
        CalenderDate cd = new CalenderDate(2022,6,13);
        System.out.println(cd.getweek());
        CalenderDate date = new CalenderDate(2022,7,9);
        boolean before = date.before(cd);
        System.out.println(before?"是":"否");
        boolean after = date.after(cd);
        System.out.println(after?"是":"否");
        int day = cd.getDate();
        System.out.println("今天是该年的第"+day+"天");
        cd.showCalender();



    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972242.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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