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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)