我有一个类DayOfWeek,它从用户那里获取日期并打印该日期的星期几.
例如,用户将输入他的日期:(使用格式MM DD YYYY)
07 22 2016
Run Code Online (Sandbox Code Playgroud)
输出应该是
星期五
这是我的班级:
public class DayOfWeek{
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
Calendar cal = Calendar.getInstance();
Date date = new Date(y, m, d);
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); …Run Code Online (Sandbox Code Playgroud)