ZOJ 2420
Calendar
A calendar is a system for measuring time, from hours and minutes, to months and days, and finally toyears and centuries. The terms of hour, day, month, year and century are all units of time measurementsof a calender system.
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, theyears 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the dateand the day of the week.
Input
The input consists of lines each containing a positive integer, which is the number of days that haveelapsed since January 1, 2000 A.D. The last line contains an integer -1, which should not be processed.You may assume that the resulting date won't be after the year 9999.
Output
For each test case, output one line containing the date and the day of the week in the format of"YYYY-MM-DD DayOfWeek", where "DayOfWeek" must be one of "Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday" and "Saturday".
Sample Input
1730
1740
1750
1751
-1
Sample Output
2004-09-26 Sunday2004-10-06 Wednesday
2004-10-16 Saturday
2004-10-17 Sunday
package data20180310;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Calendar1 {
static int month=1;
static int year=2000;
static int day=1;
static String weekDay;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int num = cin.nextInt();
while(num!=-1){
month=1;
year=2000;
day=1;
getDate(num);
num = cin.nextInt();
}
cin.close();
return ;
}
public static void getDate(int num){
String[] week={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
weekDay=week[num%7];
if(num>0){
while(num>=dayOfYear(year)){
num-=dayOfYear(year);
year++;
}
}
while(num>=dayOfMonth(month,year)){
num-=dayOfMonth(month,year);
month++;
}
day=num+day;
StringBuilder sb = new StringBuilder();
sb.append(year).append("-").append(String.format("%02d",month)).append("-").append(String.format("%02d",day));
System.out.print(sb.toString()+" ");
System.out.println(weekDay);
}
public static int dayOfYear(int year){
if(year%4==0&&year%100!=0||year%400==0){
return 366;
} else{
return 365;
}
}
public static int dayOfMonth(int month,int year){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
}
if(month==4||month==6||month==9||month==11){
return 30;
}
if(month==2){
if(year%4==0&&year%100!=0||year%400==0){
return 29;
}else{
return 28;
}
}
return 0;
}
}
本以为很简单的一道题,然后自己在做的时候一直超时,超时原因为:格式化输出的时候 先把string 转换为了Date,然后又把date转换为了str。
String str=year+"-"+month+"-"+day;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
try {
Date date = simpleDateFormat.parse(str);//转换为日期
SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd ");
System.out.print(format.format(date));//字符串转日期
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但发现不用这么麻烦,直接String.format("%2d",x) 即可输出01 02之类的。
但发现现在提交有时候也会超时,简直无语,可能是jdk1.7 和jdk1.8的原因,下次就还是最好用java了。
同时自己在写上题的时候,方法不太对,最明了的操作就是如上,先减到年,然后在减到月,最好剩下的为天。自己之前的写法有漏洞。
然后就是StringBuffer 和 String的用法:
String中的+操作慢于StringBuffer append操作,StringBuffer是字符串的引用,append直接底层操作,而+操作会产生多个String对象。
如果没有循环的情况下,单行用加号拼接字符串是没有性能损失的,java编译器会隐式的替换成stringbuilder,但循环体的情况下,并不会智能转化。
二者相互转换:
StringBuffer sb2 = new StringBuffer(s); //String转换为StringBuffer
String s1 = sb1.toString(); //StringBuffer转换为String
最后就是java.util.Date和java.sql.Date:
java.util.Date(java中使用)是java.sql.Date(数据库中使用)的父类。
将java.util.Date转化为java.sql.Date?
java.util.Date utilDate=new java.util.Date();
java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
sql.Date规范化,只有日期,没有时间。都有getTime方法返回毫秒数。