题目:
1900年1月1日到9999年12月31日中间有多少天的日历中存在二,
思路:
首先得到1900年1月1日的long值。一年是86400000毫秒,之后依次暴力枚举,每一次加86400000判断。
代码:
package 第十一届;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(1900,0,1);
Long add = 86400000L;
System.out.println(calendar.getTimeInMillis());
long time = calendar.getTimeInMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sdf.format(time));
System.out.println(sdf.format(date));
int count = 0;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
time += add;
sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(sdf.format(time));
String string = sdf.format(date);
if (string.indexOf("2")!=-1) {
count++;
System.out.println(string);
}
if (string.equals("9999-12-31")) {
break;
}
}
System.out.println(count);
}
}