首页 > 试题广场 >

日期累加

[编程题]日期累加
  • 热度指数:49252 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
设计一个程序能计算一个日期加上若干天后是什么日期。

输入描述:
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。


输出描述:
输出m行,每行按yyyy-mm-dd的个数输出。
示例1

输入

1
2008 2 3 100

输出

2008-05-13
Java
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            int year = scanner.nextInt();
            int month = scanner.nextInt();
            int day = scanner.nextInt();
            int num = scanner.nextInt();
            LocalDate date = LocalDate.of(year, month, day);
            LocalDate date1 = date.plus(num, ChronoUnit.DAYS);
            DecimalFormat f = new DecimalFormat("00");
            System.out.println(date1.getYear()+"-"+f.format(date1.getMonth().getValue())+"-"+f.format(date1.getDayOfMonth()));
        }

    }
}


发表于 2020-03-19 22:57:42 回复(0)

import java.util.Scanner;
public class Main {  public static void main(String[] args) {   Scanner sc = new Scanner(System.in);   int m = sc.nextInt();   for (int i = 0; i < m; i++) {    int year = sc.nextInt();    int month = sc.nextInt();    int day = sc.nextInt();    int s = sc.nextInt();    boolean flag=true;    while (flag) {
    switch (month) {     case 2:      if (leapYear(year)) {       if (s > 29 - day) {        s = s - (29 - day);        month++;        if (month > 12) {         month = month - 12;         year++;        }        day = 0;       } else {        day = day + s;        flag=false;       }      } else {       if (s > 28 - day) {        s = s - (28 - day);        month++;        if (month > 12) {         month = month - 12;         year++;        }        day = 0;       } else {        day = day + s;       }      }      break;     case 1:     case 3:     case 5:     case 7:     case 8:     case 10:     case 12:      if (s > 31 - day) {       s = s - (31 - day);       month++;       if (month > 12) {        month = month - 12;        year++;       }       day = 0;      } else {       day = day + s;       flag=false;      }      break;     case 4:     case 6:     case 9:     case 11:      if (s > 30 - day) {       s = s - (30 - day);       month++;       if (month > 12) {        month = month - 12;        year++;       }       day = 0;      } else {       day = day + s;       flag=false;      }      break;
    }    }             if(month<10){                 if(day<10){                     System.out.println(year+"-0"+month+"-0"+day);                 }else{                     System.out.println(year+"-0"+month+"-"+day);                 }                             }else{                 if(day<10){                     System.out.println(year+"-"+month+"-0"+day);                 }else{                     System.out.println(year+"-"+month+"-"+day);                 }             }    
  }
 }
 public static boolean leapYear(int year) {   if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {    return true;   } else {    return false;   }  } }

发表于 2019-03-19 20:09:06 回复(0)
要善于使用API,但请注意Java日历是格里高利历和罗马儒略历,一年中的第一个月是 JANUARY,它为 0,所以注意减1。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int total;
        int year, mouth, day, plus;

        int caseNum = 0;

        Calendar calendar = Calendar.getInstance();
        total = scanner.nextInt();
        while (caseNum++ < total) {
            year = scanner.nextInt();
            mouth = scanner.nextInt();
            day = scanner.nextInt();
            plus = scanner.nextInt();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, mouth - 1);
            calendar.set(Calendar.DATE, day);
            calendar.add(Calendar.DAY_OF_MONTH, plus);
            System.out.println(format.format(calendar.getTime()));
        }
    }
}

发表于 2019-03-05 17:09:25 回复(1)