- package com.itheima;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Scanner;
- /**
- *
- * 第9题:编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,
- * 并且计算出是一年中的第几天。
- *
- */
- public class Test9 {
-
- public static void main(String[] args) {
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(isr);
- String dateStr;
- try {
- DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
- Scanner scanner=new Scanner(System.in);
- String Datestring=scanner.next();
- Date in = date.parse(Datestring);
- Calendar cal = Calendar.getInstance();
- cal.setTime(in);
- int day = cal.get(Calendar.DAY_OF_MONTH); //日
- int month = cal.get(Calendar.MONTH) + 1; //月
- int year = cal.get(Calendar.YEAR ); //年
-
- //根据蔡勒(Zeller)公式 week=(y+[y/4]+[c/4]-2*c+[26(m+1)/10]+d-1)%7
- /**
- * c代表世纪减1 ,y带年份后两位,m代表月份(3<=m<=14),d代表日期
- */
- /* int m=month;
- int d=day;
- if(m>=3){
- int c=year/100;
- int y=year%100;
-
- int week=(y+y/4+c/4-2*c+26*(m+1)/10+d-1)%7;
- System.out.println(year+"-"+month+"-"+day+"是星期"+week);}
- if(m==1||m==2){
- int c=(year-1)/100;
- int y=(year-1)%100;
- System.out.println(y+" "+c);
- int week=(y+y/4+c/4-2*c+26*(m+13)/10+d-1)%7;
- System.out.println(year+"-"+month+"-"+day+"是星期"+week);
- }
- */
- //注释的这一段是计算是星期几的,下面的就是计算是第几天的
- int a[]={0,31,59,90,120,151,181,212,243,273,304,334,365};//平年每月累加天数
- int b[]={0,31,60,91,121,152,182,213,244,274,305,335,366};//闰年每月累加天数
- if( (year%4==0&&year%100!=0)||(year%400==0) ){//闰年
- int count =a[month-1]+day;
- System.out.println(year+"-"+month+"-"+day+"是"+year+"年的第"+count+"天");
- }else{
- int count=b[month-1]+day;
- System.out.println(year+"-"+month+"-"+day+"是"+year+"年的第"+count+"天");
- }
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码 |