这个Java程序,month为什么要减1呢?import java.util.*;public class PersonClass{ private int No; private String name; private boolean sex; private Date birthday; public PersonClass(){} public PersonClass(int No,String name,boo
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/24 11:00:52
这个Java程序,month为什么要减1呢?import java.util.*;public class PersonClass{ private int No; private String name; private boolean sex; private Date birthday; public PersonClass(){} public PersonClass(int No,String name,boo
这个Java程序,month为什么要减1呢?
import java.util.*;
public class PersonClass{
private int No;
private String name;
private boolean sex;
private Date birthday;
public PersonClass(){}
public PersonClass(int No,String name,boolean sex,int year,int month,int day){
this.No=No;
this.name=new String(name);
this.sex=sex;
this.birthday=new Date(year,month-1,day);
}
public void setNo(int No){this.No=No;}
public void setName(String name){this.name=new String(name);}
public void setSex(boolean sex){this.sex=sex;}
public void setBirthday(int year,int month,int day){
this.birthday=new Date(year,month-1,day);
}
public int getNo(){return No;}
public String getName(){return name;}
public boolean getSex(){return sex;}
public Date getBirthday(){return birthday;}
public boolean equals(Object otherObject){
if(this==otherObject){
return true;
}
if(otherObject==null){
return false;
}
if(getClass()!=otherObject.getClass()){
return false;
}
PersonClass other=(PersonClass)otherObject;
if(No==other.No){
return true;
}else{
return false;
}
}
public String toString(){
return"\nNo:"+No+"\nName:"+name+"\nSex:"+sex+"\nBirthday:"+String.format("%tF", birthday);
}
}
这个Java程序,month为什么要减1呢?import java.util.*;public class PersonClass{ private int No; private String name; private boolean sex; private Date birthday; public PersonClass(){} public PersonClass(int No,String name,boo
因为JDK的月份表示范围是0~11,分别表示1月至12月.
而用户习惯表示的月份往往是从1开始至12之间.
为了将用户习惯的月份表示法转换到JDK固定的表示法,故将用户表示的月份数减1,以得到JDK表示的月份数.