[UVa][日期計算][JAVA] 11356 - Dates
|
30 days
has September,
April, June and November
All the rest have 31
And February’s great with 28
And Leap Year’s February’s fine with 29
The Gregorian calendar, the current standard calendar in most part of the world, adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in -00) which are not evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900, 2100, 2200 and 2300 are not.
In this problem, we are concerned with dates. You will be given a date and an integer K. You have to find the date in the calendar after K days from the given date.
Input
The first line of input is an integer T(T<50) that represents the number of test cases. Each case contains two lines. The first line is a date in the format yyyy-month-dd. year is an integer in the range [1900, 3000], month is a string from the set {January, February, March, April, May, June, July, August, September, October, November and December} and dd is an integer in the range [01,31]. The second line contains an integer K(0<K<10000).
The input date will be a valid one.
Output
For each input, output the case number followed by the date after K days in the same format as that of input. Look at the sample for exact format.
Sample Input |
Output for Sample Input |
2 1984-December-30 2 1984-October-12 318 |
Case 1: 1985-January-01 Case 2: 1985-August-26 |
ProblemSetter: Sohel Hafiz
import java.util.Scanner;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
static String[] month = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
int cases = 0;
String a, b, c, s;
while (t-- != 0) {
s = cin.next();
int pos1 = s.indexOf('-', 0);
int pos2 = s.indexOf('-', pos1 + 1);
a = s.substring(0, pos1);
b = s.substring(pos1 + 1, pos2);
c = s.substring(pos2 + 1);
int dd = cin.nextInt(), monthnum = 0;
for (int i = 0; i < 12; i++)
if (month[i].equals(b))
monthnum = i;
GregorianCalendar date = new GregorianCalendar(Integer.parseInt(a),
monthnum, Integer.parseInt(c));
date.add(Calendar.DATE, dd);
System.out.printf("Case %d: %d-%s-%02d\n", ++cases,
date.get(Calendar.YEAR), month[date.get(Calendar.MONTH)],
date.get(Calendar.DATE));
}
}
}