java推算日期后天_分别用C++和Java写的计算后天的日期code

本文提供了一种方法来验证输入的日期格式是否正确,并计算出输入日期后的第二天具体是哪一天,包括对不同月份和闰年的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标签:#include

#include

#include

using namespace std;

bool checkNum(char c){

if(c >='0' && c <='9')

return true;

return false;

}

bool checkString(string str){

if(checkNum(str[0])&&checkNum(str[1])&&checkNum(str[2])

&&checkNum(str[3])&&(str[4]=='-')&&checkNum(str[5])

&&checkNum(str[6])&&(str[7]=='-')&&checkNum(str[8])&&checkNum(str[9]))

return true;

return false;

}

int main(){

string str;

int year;

int month;

int day;

while(cin>>str){

int flag = 1;

if(!checkString(str)){

flag=0;

cout<

}else{

year=(str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+(str[3]-'0');

month=(str[5]-'0')*10+(str[6]-'0');

day=(str[8]-'0')*10+(str[9]-'0');

if(month==1 || month==3 || month==5 || month==7

|| month==8 || month==10 || month==12){

if(day >=1 && day <=31){

day += 2;

if(day > 31){

month += 1;

day = 1;

}

if(month > 12){

year += 1;

month = 1;

}

}else{

flag=0;

cout<

}

}

else if(month==4 || month==6 || month==9 || month==11){

if(day >=1 && day <=30){

day += 2;

if(day > 30){

month += 1;

day = 1;

}

if(month > 12){

year += 1;

month = 1;

}

}else{

flag=0;

cout<

}

}

else if(month == 2){

if((year%4==0 && year%100!=0) || (year%400==0)){

//如果为闰年

if(day >=1 && day <=29){

day += 2;

if(day > 29){

month += 1;

day = 1;

}

if(month > 12){

year += 1;

month = 1;

}

}else{

flag=0;

cout<

}

}

else{

if(day >=1 && day <=28){

day += 2;

if(day > 28){

month += 1;

day = 1;

}

if(month > 12){

year += 1;

month = 1;

}

}else{

flag=0;

cout<

}

}

}

else{

cout<

}

}

if(flag==1){

cout<

}

}

return 0;

}

c4c39e5b392ba1a1fa0d6960ecf2799b.png

import java.util.Scanner;

public class CalculateTheDayAfterTomorrow {

public static boolean checkNum(char c) {

// 判断当前字符是否为数字

if (c >= '0' && c <= '9')

return true;

return false;

}

public static boolean checkString(String str) {

// 判断字符串是否为指定的日期格式:yyyy-mm-dd

if (checkNum(str.charAt(0)) && checkNum(str.charAt(1))

&& checkNum(str.charAt(2)) && checkNum(str.charAt(3))

&& (str.charAt(4) == '-') && checkNum(str.charAt(5))

&& checkNum(str.charAt(6)) && (str.charAt(7) == '-')

&& checkNum(str.charAt(8)) && checkNum(str.charAt(9)))

return true;

return false;

}

public static void main(String args[]) {

String str;

int year = 0;

int month = 0;

int day = 0;

Scanner scanner = new Scanner(System.in);

while ((str = scanner.nextLine()) != null) {

int flag = 1;

if (!checkString(str)) {

flag = 0;

System.out.println("您输入的日期格式不符合要求" + "\n");

} else {

// 计算年月日

year = (str.charAt(0) - '0') * 1000 + (str.charAt(1) - '0')

* 100 + (str.charAt(2) - '0') * 10

+ (str.charAt(3) - '0');

month = (str.charAt(5) - '0') * 10 + (str.charAt(6) - '0');

day = (str.charAt(8) - '0') * 10 + (str.charAt(9) - '0');

// 每个月的天数不一样,分类讨论

if (month == 1 || month == 3 || month == 5 || month == 7

|| month == 8 || month == 10 || month == 12) {

if (day >= 1 && day <= 31) {

day += 2;

if (day > 31) { // 已经跨月了

month += 1;

day -= 31;

}

if (month > 12) { // 后两天跨年了

year += 1;

month = 1;

}

} else {

flag = 0;

System.out.println("您输入的日期不存在" + "\n");

}

} else if (month == 4 || month == 6 || month == 9

|| month == 11) {

if (day >= 1 && day <= 30) {

day += 2;

if (day > 30) {

month += 1;

day -= 30;

}

if (month > 12) {

year += 1;

month = 1;

}

} else {

flag = 0;

System.out.println("您输入的日期不存在" + "\n");

}

} else if (month == 2) {

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

// 如果为闰年

if (day >= 1 && day <= 29) {

day += 2;

if (day > 29) {

month += 1;

day -= 29;

}

if (month > 12) {

year += 1;

month = 1;

}

} else {

flag = 0;

System.out.println("您输入的日期不存在" + "\n");

}

} else {// 平年

if (day >= 1 && day <= 28) {

day += 2;

if (day > 28) {

month += 1;

day -= 28;

}

if (month > 12) {

year += 1;

month = 1;

}

} else {

flag = 0;

System.out.println("您输入的日期不存在" + "\n");

}

}

} else {

flag = 0;

System.out.println("您输入的月份不存在" + "\n");

}

}

if (flag == 1) { // 通过flag标志输入的日期是否合法

System.out.println("后天的日期为: " + year + "年" + month + "月" + day

+ "日" + "\n");

}

}

}

}

5db7c03bf5902f88ffe52432d690a021.png

标签:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值