3564. 日期类
编写一个日期类,要求按 xxxx-xx-xx
的格式输出日期,实现加一天的操作。
输入格式
第一行包含整数 TT,表示共有 TT 组测试数据。
每组数据占一行,包含 33 个用空格隔开的整数,分别表示年月日。
输出格式
每组数据输出一行,一个结果,按 xxxx-xx-xx
的格式输出,表示输入日期的后一天的日期。
数据范围
输入日期保证合法且不会出现闰年。
年份范围 [1000,3000][1000,3000]
输入样例:
2
1999 10 20
2001 1 31
输出样例:
1999-10-21
2001-02-01
题解:
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while (n --> 0){
int year, month, day;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
if (++day <= arr[month]){}
//如果当前日期等于最后一天,就月份加1
else {
day = 1;
//如果当前月份超过12月就年份加1
if (++month == 13) {
month = 1;
year++;
}
}
bw.write(String.format("%04d-%02d-%02d\n", year, month, day));
bw.flush();
}
}
}
评论区