定义一个满足下列要求的 Date 类:
①用下面的格式输出日期:月/日/年 ;
②可运行在日期上加一天操作
③设置日期
最简单的代码:
public class Date
{
private int day;
private int month;
private int year;
/** *日期赋值 */
public Date(int day,int month,int year){
this.day = day;
this.month = month;
this.year = year;
}
/** * 格式化输出 */
public void formatDate() {
System.out.println(day + "/" + month + "/" + year);
}
/** * 加一天 */
public void addOneDay() {
day++;
int max = isValid();
if(day>max){
++month;
day%=max;
}
if (month / 13 != 0)
++year;
if ((month %= 13) == 0)
++month;
formatDate();
}
/** * 判断是否闰年 */
public boolean isLeapYear() {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
/**判断该月最多有多少天*/
private int isValid() {
int max=31;
switch (month)
{
case 4:
case 6:
case 9:
case 11: max--;break;
case 2:max=isLeapYear()?29:28; }
return max;
}
/** * 主方法 * */
public static void main(String[] args) {
Date date = new Date(29,2,2020);
date.formatDate();
date.addOneDay();
}
}
需要输入日期,但无法判断输入对错:
import java.util.Scanner;
public class Date02
{
private int day;
private int month;
private int year;
public void format() {
System.out.println(day + "/" + month + "/" + year);
}
public void plus() {
switch (month) {
case 4:
case 6:
case 9:
case 11:
if (day / 30 != 0)
month++;
day %= 30;
break;
case 2:
if (isLeapYear()) {
if (day / 29 != 0) ++month;
day %= 29;
} else {
if (day / 28 != 0) ++month;
day %= 28;
} break;
default: if (day / 31 != 0) ++month;
day %= 31; break; }
if (month / 13 != 0)
++year;
if ((month %= 13) == 0)
++month;
++day;
format();
}
public boolean isLeapYear() {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份");
this.year = scanner.nextInt();
System.out.println("请输入月份");
this.month= scanner.nextInt();
System.out.println("请输入日期");
this.day = scanner.nextInt();
}
public static void main(String[] args) {
Date02 date = new Date02();
date.input();
date.format();
date.plus();
}
}
需要输入日期,并且可以判断输入对错:
import java.util.Scanner;
/** * 日期操作 * @author JOKER * */
public class Date
{
private int day;
private int month;
private int year;
/** * 格式化输出 */
public void formatDate() {
System.out.println(day + "/" + month + "/" + year);
}
/** * 加一天 */
public void plusDate() {
/*switch (month) {
case 4:
case 6:
case 9:
case 11: if (day / 30 != 0)
month++;
day %= 30;
break;
case 2:
if (isLeapYear()) {
if (day / 29 != 0) ++month; day %= 29;
} else {
if (day / 28 != 0) ++month;
day %= 28;
} break;
default: if (day / 31 != 0) ++month;
day %= 31; break; }*/
day++;
int max = isValid();
if(day>max){
++month;
day%=max;
}
if (month / 13 != 0)
++year;
if ((month %= 13) == 0)
++month;
formatDate();
}
/** * 判断是否闰年 */
public boolean isLeapYear() {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
/** * 输入 */
public void input() {
Year();
Month();
Day();
}
/** * 年份输入*/
public void Year(){
System.out.println("请输入年份");
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt()){
this.year = scanner.nextInt();
if(year<=0){
System.out.println("年份输入错误!!!请重新输入!");
Year();
}
}else{
System.out.println("年份输入错误!!!请重新输入!");
Year();
}
}
/** * 月份输入 */
public void Month(){
System.out.println("请输入月份");
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt()){
this.month = scanner.nextInt();
if(month>12||month<1){
System.out.println("月份输入错误!!!请重新输入!");
Month();
}
}else{
System.out.println("月份输入错误!!!请重新输入!");
Month();
}
}
/** * 日期输入*/
public void Day(){
System.out.println("请输入日期");
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt()){
this.day = scanner.nextInt();
if(day>isValid() || day<=0){
System.out.println("日期输入错误!!!请重新输入!");
Day();
}
}else{
System.out.println("日期输入错误!!!请重新输入!");
Day();
}
}
/**判断该月最多有多少天*/
private int isValid() {
int max=31;
switch (month)
{
case 4:
case 6:
case 9:
case 11: max--;break;
case 2:max=isLeapYear()?29:28; }
return max;
}
/** * 主方法 * */
public static void main(String[] args) {
Date date = new Date();
date.input();
date.formatDate();
date.plusDate();
}
}