package com.heim.zuoye;
import java.util.Calendar;
import java.util.Scanner;
public class Test3 {
/**
* (1)通过键盘录入日期字符串,格式(2015-10-20)
(2)输出该日期是星期几及这一年的第几天
(3)如输入:2015-10-20,输出"2015年-10月-20日 是 星期二,是 2015年的第 293 天"
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入日期字符串,格式(2015-10-20):");
String line = sc.nextLine();
String[] s = line.split("-");
int[] arr = new int[s.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(s[i]);
}
Calendar c = Calendar.getInstance();
c.set(arr[0], arr[1], arr[2]);
c.get(Calendar.DAY_OF_YEAR);
System.out.println(c.get(Calendar.YEAR) + "-" + getNum((c.get(Calendar.MONTH)+1))
+ "-" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "-" + getWeek(c.get(Calendar.DAY_OF_WEEK))
+"第" + c.get(Calendar.DAY_OF_YEAR) + "天");
}
public static String getWeek(int week) {
String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return arr[week];
}
public static String getNum(int num) {
return num > 0 ? "" + num : "0" + num;
}
}
|
|