qq_38692022 2018-11-23 10:26 采纳率: 70%
浏览 815
已采纳

java用类与对象写两个天数之差

求大神帮我看看 这个该怎么写啊,最好有个代码让我学习一下。。。

编写日期类Date,要求如下:

1)成员变量:year,month,day,类型int

2)默认构造方法Date()。默认值为2000.1.1

3)构造方法Date(int year,int month,int day)

4) 编写计算闰年的任意两个日期对象之间相隔天数的方法int interval(Date d)

  • 写回答

2条回答 默认 最新

  • 星星星小冷猫 2018-11-23 10:46
    关注

    不知符合你要求没.计算任意两个日期之间的天数..部分代码命名不用在意。理解起来应该很容易

     //main方法
     public static void main(String[] args) throws ParseException {
            DateDemo dateDemo = new DateDemo();
            DateDemo dateDemo1 = new DateDemo(2004,1,1);
            Integer interval = interval(dateDemo);
            Integer interval1 = interval(dateDemo1);
            System.out.println(interval1-interval);
        }
    
        public static Integer interval(DateDemo dateDemo) throws ParseException {
            Integer day = dateDemo.getDay();
            Integer month = dateDemo.getMonth();
            Integer year = dateDemo.getYear();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = sdf.parse(year + "-" + month + "-" + day);
            long time = parse.getTime();
            long l = time / (1000*60*60*24);
            String s = String.valueOf(l);
            return Integer.valueOf(s);
        }
    }
    
    //Date类
     public class DateDemo {
        private Integer year = 2000; 
        private Integer month = 1;
        private Integer day = 1;
    
        public DateDemo(Integer year, Integer month, Integer day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    
        public DateDemo() {
        }
    
        public Integer getYear() {
            return year;
        }
    
        public void setYear(Integer year) {
            this.year = year;
        }
    
        public Integer getMonth() {
            return month;
        }
    
        public void setMonth(Integer month) {
            this.month = month;
        }
    
        public Integer getDay() {
            return day;
        }
    
        public void setDay(Integer day) {
            this.day = day;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值