完善3-32的MyDate类

package MyDate;
public class MyDate{
private int year,month,day;
private static int thisYear;
static
{
thisYear=2012;
}
public MyDate(int year,int month,int day)
{
this.set(year,month,day);
}
public MyDate()
{
this(1970,1,1);
}
public MyDate(MyDate d)
{
this.set(d);
}
public void set(int year,int month,int day)
{
this.year=year;
this.month=(month>=1&&month<=12)?month:1;
this.day=(day>=1&&day<=31)?day:1;
}
public void set(MyDate d)
{
set(d.year,d.month,d.day);
}
public int getYear()
{
return this.year;
}
public int getMonth()
{
return this.month;
}
public int getDay()
{
return this.day;
}
public String toString()
{
return year+"年"+String.format("%02d", month)+"月"+String.format("%2d",day)+"日";
}
public static int getThisyear()
{
return thisYear;
}
public static boolean isLeapYear(int year)
{
return year%400==0||year%100!=0&&year%4==0;
}
public boolean isLeapYear()
{
return isLeapYear(this.year);
}
public boolean equals(MyDate d)
{
return this==d||d!=null&&this.year==d.year&&this.month==d.month&&this.day==d.day;
}
public static int daysOfMonth(int year,int month)
{
switch(month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12: return 31;
case 4:case 6:case 9:case 11: return 30;
case 2:return MyDate.isLeapYear(year)?29:28;
default: return 0;
}
}
public int daysOfMonth()
{
return daysOfMonth(this.year,this.month);
}
public void tomorrow()
{
this.day++;
if(this.day>this.daysOfMonth())
{
this.day=1;
this.month++;
if(this.month>12)
{
this.month=1;
this.year++;
}
}
}
public MyDate yesterday()
{
MyDate date=new MyDate(this);
date.day--;
if(date.day==0)
{
date.month--;
if(date.month==0)
{
date.month=12;
date.year--;
}
date.day=daysOfMonth(date.year,date.month);
}
return date;
}
}
class MyDate_ex
{
public static void main(String args[])
{
System.out.println("今年是"+MyDate.getThisyear()+",闰年?"+MyDate.isLeapYear(MyDate.getThisyear()));
MyDate d1=new MyDate(2012,12,31);
MyDate d2=new MyDate(d1);
System.out.println("d1:"+d1+",d2:"+d2+",d1==d2?"+(d1==d2)+",d1.equals(d2)?"+d1.equals(d2));
System.out.print(d1+"的明天是");
d1.tomorrow();
System.out.println(d1+"\n"+d1+"的昨天是"+(d2=d1.yesterday()));
}
}

时间: 2024-08-01 23:01:28

完善3-32的MyDate类的相关文章

Python接口测试实战4(下) - 框架完善:用例基类,用例标签,重新运行上次失败用例

如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战2 - 使用Python发送请求 Python接口测试实战3(上)- Python操作数据库 Python接口测试实战3(下)- unittest测试框架 Python接口测试实战4(上) - 接口测试框架实战 Python接口测试实战4(下) - 框架完善:用例基类,用例标签,重新运行上次失败用例

iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值

http://www.cocoachina.com/ios/20150807/12877.html 在上篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property Name相同,然后通过运行时来生成和执行Setter方法给Model类的属性赋值. 通过Runtime来给Model类属性赋值的好处是多多的,它便于代码的后期维护,并且提高了开发效率.当你拿到解析后的字典时你

一套完善的Android异步任务类

欢迎各位加入我的Android开发群[257053751] 今天向大家介绍一个很有用的异步任务类处理类,分别包含了AsyncTask各个环节中的异常处理.大量并发执行而不发生异常.字符串数据缓存等功能.并且感谢@马天宇(http://litesuits.com/)给我的思路与指点. 研究过Android系统源码的同学会发现:AsyncTask在android2.3的时候线程池是一个核心数为5线程,队列可容纳10线程,最大执行128个任务,这存在一个问题,当你真的有138个并发时,即使手机没被你撑

iOS开发&gt;学无止境 - 遍历Model类的属性并完善使用Runtime给Model类赋值

在前几天的一篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property Name相同,然后通过运行时来生成和执行Setter方法给Model类的属性赋值. 通 过Runtime来给Model类属性赋值的好处是多多的,它便于代码的后期维护,并且提高了开发效率.当你拿到解析后的字典时你不用一个一个的通过 key去把字典的值赋值给相应的Model类的属性,本篇博客中会给出如何

Java基础知识强化32:String类之String的面试题

1.先看一个图: 2.String面试题: (1)题1: 1 package cn.itcast_02; 2 3 /* 4 * 看程序写结果 5 */ 6 public class StringDemo3 { 7 public static void main(String[] args) { 8 String s1 = new String("hello"); 9 String s2 = new String("hello"); 10 System.out.pri

iOS-MD5加密32位扩展类

NSString+MD5.h // // NSString+MD5.h // Spread // // Created by 邱学伟 on 16/4/26. // Copyright ? 2016年 邱学伟. All rights reserved. // #import <Foundation/Foundation.h> @interface NSString (MD5) /** 将字符串经MD5加密 */ +(NSString *)MD5:(NSString *)str; @end NSS

Java类的设计----Object 类

Object类 Object类是所有Java类的根父类如果在类的声明中未使用extends关键字指明其父类,则默认父类为Object类 public class Person { ... } 等价于: public class Person extends Object {. ... }例: method(Object obj){-}//可以接收任何类作为其参数 Object o=new Person; method(o); ==操作符与equals方法 ==操作符与equals方法的区别: =

Quick Cocos2dx 场景对象基类实现

从使用Quick-Cocos2d-x搭建一个横版过关游戏(四)拷来个进度条类, 但是由于那个类有个bug,在setProgress里面self.fill是找不到的,所以我改进了一下,代码如下: 1 local Progress = class("Progress", function() 2 -- body 3 return display.newNode() 4 end) 5 6 function Progress:ctor(background, fill) 7 local pro

java之 ------ 类与对象

MyDate类 public class MyDate{ private int year, month, day; private static int thisYear=2015; public MyDate(int year, int month, int day){ this.year=year; this.month=month; this.day = day; } public MyDate(){ this(1970,1,1); //Date new Date()--> long }