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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?