按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天。
package com.itheima.day11;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class CalendarDemo {
/**
* @param args
* @throws ParseException
*
*/
public static void main(String[] args) throws ParseException {
System.out.println("输入日期,格式是,例如:2013-12-3");
Scanner sc = new Scanner(System.in);
String str_date = sc.nextLine();
showDate(str_date);
sc.close();
}
private static void showDate(String str_date) throws ParseException {
DateFormat dateFormat = DateFormat.getDateInstance();
Date date = dateFormat.parse(str_date);
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DAY_OF_YEAR);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(str_date+",在这一天是"+getWeek(week)+",是这一年中的第"+day+"天");
}
private static String getWeek(int week) {
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
return weeks[week];
}
} |