[Java] 纯文本查看 复制代码 public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("通过键盘录入日期字符串,格式:2015-10-20");
String date = sc.nextLine();
//String date = "2015-10-20";//将录入的字符串切割
String[] arr = date.split("-");
Calendar c = Calendar.getInstance();
//修改年月日,月份是从0开始编号的,于是要-1
c.set(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]) - 1, Integer.parseInt(arr[2]));
//获取星期中的第几天,周日为第一天
int week_day = c.get(Calendar.DAY_OF_WEEK);
//获取一年中的第几天
int year_day = c.get(Calendar.DAY_OF_YEAR);
System.out.println(arr[0] + "年-" + arr[1] + "月-" + arr[2] + "日 是" + getWeek(week_day) + arr[0]
+ "年的第" + year_day + "天"
);
}
//将星期中的第几天转换为相应的星期
public static String getWeek(int week_day) {
String week = null;
switch (week_day - 1) {
case 0:
week = "星期天";
break;
case 1:
week = "星期一";
break;
case 2:
week = "星期二";
break;
case 3:
week = "星期三";
break;
case 4:
week = "星期四";
break;
case 5:
week = "星期五";
break;
case 6:
week = "星期六";
break;
}
return week;
}
}
|