- public static void main(String[] args) throws IOException{
- [code]public static void main(String[] args) throws IOException{
- //通过键盘如果日期
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
- String buf = null;
- while((buf = br.readLine())!= null){
- //定义正则表达式
- String regex = "[0-9]{4}-(0[1-9]|1[0-2])-([0][1-9]|[1-2][0-9]|3[0-1])";
-
- if(!buf.matches(regex)){
- System.out.println("日期不合法");
- continue;
- }
- int year = Integer.parseInt(buf.substring(0, 4));
- int month = Integer.parseInt(buf.substring(5, 7));
- int day = Integer.parseInt(buf.substring(8, 10));
- System.out.println(year+"年:"+month+"月:"+day+"日");
- Calendar cal = Calendar.getInstance();
- cal.set(year, month, day);
- int weekNum = cal.get(Calendar.DAY_OF_WEEK);
- int months = cal.get(Calendar.MONTH);
- String week = getWeek(weekNum);
- System.out.println("星期数:"+weekNum);
- System.out.println(week);
- System.out.println(months+"月");
-
- }
-
-
-
- }
- public static String getWeek(int weekNum) {
- String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
- return weeks[weekNum];
-
- }
- }
复制代码
当我运行的时候输入:2014-09-02
打印的结果是:
2014年:9月:2日
星期数:5
星期四
9月
为什么星期数DAY_OF_WEEK 的值是5啊??
哪位大神帮忙看一下,谢谢啊
|
|