//取当前日期,通常使用Date
Date date = new Date();
// System.out.println(date);
//使用SimpleDateFormat类进行格式化输出
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh:mm:ss E");
String dateMsg = sdf.format(date);
// System.out.println(dateMsg);
//当涉及到具体某个日期的设置或获取的时候,通常使用Calendar
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份");
int year = scan.nextInt();
System.out.println("请输入月份");
int month = scan.nextInt();
System.out.println("请输入日期");
int day = scan.nextInt();
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
String weekDay = null;
switch(cal.get(Calendar.DAY_OF_WEEK)){
case Calendar.MONDAY:
weekDay = "一";
break;
case Calendar.TUESDAY:
weekDay = "二";
break;
case Calendar.THURSDAY:
weekDay = "三";
break;
case Calendar.WEDNESDAY:
weekDay = "四";
break;
case Calendar.FRIDAY:
weekDay = "五";
break;
case Calendar.SATURDAY:
weekDay = "六";
break;
case Calendar.SUNDAY:
weekDay = "天";
break;
}
System.out.println("那天是星期" + weekDay);
}
Date date = new Date();
// System.out.println(date);
//使用SimpleDateFormat类进行格式化输出
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh:mm:ss E");
String dateMsg = sdf.format(date);
// System.out.println(dateMsg);
//当涉及到具体某个日期的设置或获取的时候,通常使用Calendar
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份");
int year = scan.nextInt();
System.out.println("请输入月份");
int month = scan.nextInt();
System.out.println("请输入日期");
int day = scan.nextInt();
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
String weekDay = null;
switch(cal.get(Calendar.DAY_OF_WEEK)){
case Calendar.MONDAY:
weekDay = "一";
break;
case Calendar.TUESDAY:
weekDay = "二";
break;
case Calendar.THURSDAY:
weekDay = "三";
break;
case Calendar.WEDNESDAY:
weekDay = "四";
break;
case Calendar.FRIDAY:
weekDay = "五";
break;
case Calendar.SATURDAY:
weekDay = "六";
break;
case Calendar.SUNDAY:
weekDay = "天";
break;
}
System.out.println("那天是星期" + weekDay);
}