您的位置:首页 > 编程语言 > Java开发

Day of Week

2014-05-08 01:12 399 查看
题目描述:

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.

For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.

Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
输入:

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出:

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
样例输入:
9 October 2001
14 October 2001

样例输出:
Tuesday
Sunday

提示:

Month and Week name in Input/Output:

January, February, March, April, May, June, July, August, September, October, November, December

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturdayimport java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main{

/**
* @param args
*/
public static void main(String[] args) {
Map<String,Integer> monthMap = new HashMap<String, Integer>();
monthMap.put("January", 1);
monthMap.put("February", 2);
monthMap.put("March", 3);
monthMap.put("April", 4);
monthMap.put("May", 5);
monthMap.put("June", 6);
monthMap.put("July", 7);
monthMap.put("August", 8);
monthMap.put("September", 9);
monthMap.put("October", 10);
monthMap.put("November", 11);
monthMap.put("December", 12);
Map<Integer,String> weekMap = new HashMap<Integer,String>();
weekMap.put(1, "Monday");
weekMap.put(2, "Tuesday");
weekMap.put(3, "Wednesday");
weekMap.put(4, "Thursday");
weekMap.put(5, "Friday");
weekMap.put(6, "Saturday");
weekMap.put(7, "Sunday");

Scanner scanner = new Scanner(System.in);

while( scanner.hasNext() ){
String date = scanner.nextLine();

date = formatDate(date,monthMap);

String today = "20140507";
int resultDayOfWeek = -1;

if( cmpDate(today,date) > 0 ){
int days = getDaysNumBetween(date,today);
resultDayOfWeek = days % 7 + 3;
if(resultDayOfWeek > 7) resultDayOfWeek = resultDayOfWeek - 7;
}else if(cmpDate(today,date) < 0 ){
int days = getDaysNumBetween(today,date);
resultDayOfWeek = 3 - days % 7;
if(resultDayOfWeek <= 0) resultDayOfWeek = resultDayOfWeek + 7;

}else if(cmpDate(today,date) == 0 ){
resultDayOfWeek = 3;
}

System.out.println(weekMap.get(resultDayOfWeek));

}
}

private static String formatDate(String date, Map<String,Integer> monthMap) {

String temp[] = date.split(" ");

if(temp[0].length() == 1){
temp[0] = "0" + temp[0];
}
temp[1] = monthMap.get(temp[1]).toString();
if(temp[1].length() == 1){
temp[1] = "0" + temp[1];
}

return temp[2]+temp[1]+temp[0];

}

private static int getDaysNumBetween(String date1, String date2) {
int year1 = Integer.parseInt( date1.substring(0, 4) );
int month1 = Integer.parseInt( date1.substring(4, 6) );
int day1 = Integer.parseInt( date1.substring(6, 8) );
int year2 = Integer.parseInt( date2.substring(0, 4) );
int month2 = Integer.parseInt( date2.substring(4, 6) );
int day2 = Integer.parseInt( date2.substring(6, 8) );
int count = 0;
while( !(year1 == year2 && month1 == month2 && day1 == day2)){
if( day2 < getDayNum(year2,month2)){
day2 ++;
count ++;
}else{
if(month2 < 12){
month2 ++;
day2 = 1;
}else{
year2++;
month2 = 1;
day2 = 1;
}
count ++;
}

}
return count;
}

public static int cmpDate(String date1, String date2){
int year1 = Integer.parseInt( date1.substring(0, 4) );
int month1 = Integer.parseInt( date1.substring(4, 6) );
int day1 = Integer.parseInt( date1.substring(6, 8) );
int year2 = Integer.parseInt( date2.substring(0, 4) );
int month2 = Integer.parseInt( date2.substring(4, 6) );
int day2 = Integer.parseInt( date2.substring(6, 8) );

if(year2 > year1){
return 1;
}else if(year2 < year1){
return -1;
}else if(month2 > month1){
return 1;
}else if(month2 < month1){
return -1;
}else if(day2 > day1){
return 1;
}else if(day2 < day1){
return -1;
}else{
return 0;
}

}

public static int getDayNum(int year, int month){
if(month == 1 ||
month == 3 ||
month == 5 ||
month == 7 ||
month == 8 ||
month == 10 ||
month == 12){
return 31;
}else if(month == 2){
if(isLeapYear(year)){
return 29;
}else{
return 28;
}
}else{
return 30;
}

}

private static boolean isLeapYear(int year) {
return (year%4 == 0 && year%100 != 0) || year%400 == 0;
}

}

/**************************************************************
Problem: 1043
User: yihukurama
Language: Java
Result: Accepted
Time:110 ms
Memory:17236 kb
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leap