java中输入一个日期

2024-11-16 04:28:54
推荐回答(2个)
回答(1):

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//第几周
int week = calendar.get(Calendar.WEEK_OF_MONTH);
//第几天,从周日开始
int day = calendar.get(Calendar.DAY_OF_WEEK);

然后根据这两个值自己判断输出吧

回答(2):

public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("请输入:");
Date date = simpleDateFormat.parse(input.nextLine());
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
int week=calendar.get(Calendar.DAY_OF_WEEK);
if (week==1) {
//第几个星期
int weekNum=calendar.get(Calendar.WEEK_OF_MONTH);
switch (weekNum) {
case 1:
System.err.println(1);
break;
case 2:
System.err.println(2);
break;
case 3:
System.err.println(3);
break;
case 4:
System.err.println(4);
break;
case 5:
System.err.println(5);
break;
}
}else{
System.err.println(0);
}
}