【Java设计一个类(日期类)

根据需求列出功能:
 *  1. 传 年、月、日 构造日期类
 *  2. 在当前日期上增加多少天
 *  3. 在当前日期上减少多少天
 *  4. 可以返回字符串 String 的方法  ”2019-05-20*  5. 加一些限制,年支持的范围 [1900, 2100]
 *  6. 如果给定两个日期,希望计算下两个日期之间相差多少天
public class Date {
 private int year;
 private int month;
 private int day; 
 // 构造方法
 public Date(int year, int month, int day) {
  // 至少做基本的参数检查
  if (year < 1900 || year > 2100) {
   // 最好的做法是抛异常
   System.out.println("年不合法: " + year);
   return;
  }  
  if (month < 1 || month > 12) {
   System.out.println("月不合法: " + month);
   return;
  }  
  if (day < 1 || day > getDayOfMonth(year, month)) {
   System.out.println("日不合法: " + day);
   return;
  }  
  // 涉及 name shadow
  this.year = year;
  this.month = month;
  this.day = day;
 } 
 private Date(Date other) {
  this(other.year, other.month, other.day);
 } 
 // 支持的方法
 public void add(int days) {
  // days 必须是正数
  if (days < 0) {
   System.out.println("days 不合法: " + days);
   return;
  }
  
  day += days;
  while (day > getDayOfMonth(year, month)) {
   day -= getDayOfMonth(year, month);
   month++;
   if (month > 12) {
    month = 1;
    year++;
   }
  }
 } 
 public void sub(int days) {
  // days 必须是正数
  if (days < 0) {
   System.out.println("days 不合法: " + days);
   return;
  }  
  day -= days;
  while (day < 1) {
   month--;
   if (month < 1) {
    month = 12;
    year--;
   }
   day += getDayOfMonth(year, month);
  }
 } 
 public String toString() {
  return String.format("%04d-%02d-%02d", year, month, day);
 } 
 // 返回 d1 和 d2 之间相差的天数
 // 要求 d1 必须大于 d2
 public static int differ(Date d1, Date d2) {
  if (!isGreatThan(d1, d2)) {
   System.out.println("必须 d1 大于 d2");
   return -1;
  }
  
  int days = 0;
  Date tmp = new Date(d1);
  while (isGreatThan(tmp, d2)) {
   tmp.sub(1);
   days++;
  } 
  return days;
 }
 // 内部使用的方法,private
 private static final int[] DAY_OF_MONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 private static int getDayOfMonth(int year, int month) {
  int days = DAY_OF_MONTH[month - 1];
  if (month == 2 && isLeapYear(year)) {
   days = 29;
  }
  
  return days;
 } 
 private static boolean isLeapYear(int year) {
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
   return true;
  }
  
  return false;
 } 
 private static boolean isGreatThan(Date d1, Date d2) {
  if (d1.year > d2.year) {
   return true;
  }
  
  if (d1.year == d2.year && d1.month > d2.month) {
   return true;
  }
  
  if (d1.year == d2.year && d1.month == d2.month && d1.day > d2.day) {
   return true;
  }
  
  return false;
 }
 // 入口方法
 public static void main(String[] args) {
  Date date = new Date(2019, 5, 20);
  System.out.println(date.toString());
  date.add(10);
  System.out.println(date.toString());
  date.add(10);
  System.out.println(date.toString());
  date.sub(22);
  System.out.println(date.toString());  
  Date today = new Date(2019, 5, 20);
  Date intern = new Date(2019, 12, 1); 
  System.out.printf("相差 %d 天%n", 
   Date.differ(intern, today));
 }
}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢唱跳有错吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值