JAVA给定日期,计算几天后,几天前的日期——Date类,体会类和对象。

/*
 * Date 	存储 年-月-日 信息
 * 原则: 一切从用户角度出发
 * 功能:
 *		1) 初始化
 *			i.	传入年/月/日
 *			2.  不传,今天
 *		2) 多少天之后的年/月/日
 *		3) 多少天之前的年/月/日
 */
public class Date {
   private int year;  //属性
   private int month;
   private int day;
   private static int[] day_of_month = {31,28,31,30,31,30,31,31,30,31,30,31};
   //年的支持范围为[1840,2025]
    //月的支持范围为[1,12]
    //年支持的范围根据月份
   public Date(int year,int month,int day){//构造方法
    //对用户传入的参数进行合法性校验
    if(year < 1840|| year > 2025){
        System.err.println("年的支持范围为1840-2025");
    }
    if(month < 1|| month > 12 ){
        System.err.println("月的支持范围为1-12");
    }
    if(day < 0 || day > dayOfmonth(month,year)){
        System.err.println("天数不正确");
    }
    this.year = year;
    this.month = month;
    this.day = day;
   }
   public static int dayOfmonth(int month,int year){
       if(month != 2){
           return day_of_month[month - 1];
       }
       else if(is_Leapyear(year)){
           return 29;
       }
       else{
           return 28;
       }
   }
  private static boolean is_Leapyear(int year){
       if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
           return true;
       }
       else{
           return false;
       }
  }
    public Date after(int days){  //会改变原先的日期
    day+=days;
    while(day > dayOfmonth(month,year)){
        month ++;
        if(month > 12){
            year ++;
        }
    }
    return this;
    }
    public Date before(int days){ //会改变原先的日期
     day-=days;
     while(day < 0){
         month--;
         day += dayOfmonth(month,year);
         if(month < 1){
             month = -month;
             year --;
         }
     }
     return this;
    }
    public Date immutableAfter(int days) {
        Date other = new Date(year,month,day);
        // 修改的是 other 的属性,计算完多少天后的日期后,原来的日期不会改变
        other.day = day + days;
        while(other.day > dayOfmonth(other.month,other.year)){
            other.month ++;
            if(other.month > 12){
                other.year ++;
            }
        }
        return other;
    }
    public Date immutablebefore(int days){
       Date other = new Date(year,month,day);
         other.day = day - days;
        while(other.day < 0){
            other.month--;
             other.day += dayOfmonth( other.month, other.year);
            if( other.month < 1){
                 other.month = -other.month;
                other.year --;
            }
        }
        return other;
    }
   public String toString(){
       return String.format("%04d-%02d-%02d",year,month,day);
   }
    public static void main(String[] args) {
        Date a = new Date(2017,8,15);
        Date r = a.after(15);  //2017-8-30
        System.out.println(r.toString());
        Date m = a.before(10); //2017-8-20 这是8-30的前十天,因为a已经改变了
        System.out.println(m.toString());
        Date b = new Date(2017,7,15);
        System.out.println(b.toString());
        Date s = b.immutableAfter(5);  //2017-7-20
        System.out.println(s.toString());
        Date q = b.immutablebefore(10);  //2017-7-5
       System.out.println(q.toString());
        System.out.println(b.toString());
    }
  }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值