package com.heim.zuoye;
import java.util.Calendar;
import java.util.Scanner;
public class Test4 {
/**
* (1)通过键盘录入日期字符串,格式(2015-10-20)
(2)定义方法获取该日期的前一天
(3)定义方法获取该日期的上个月份
(4)定义方法获取该日期的所在月份的第一天
(5)定义方法获取该日期的所在月份的最后一天
*/
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.add(Calendar.DAY_OF_YEAR, -1);
c.add(Calendar.MONTH, -2);
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;
}
public static String getYear(int year) {
return "";
}
}
|
|