一、我的
import javax.imageio.plugins.tiff.ExifTIFFTagSet;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Year;
import java.util.Scanner;
public class Main {
public static String getWeekday(int year, int month, int day) {
LocalDate date = LocalDate.of(year, month, day);
DayOfWeek dayOfWeek = date.getDayOfWeek();
return dayOfWeek.toString();
}
public static void main(String[] args) {
int year=0;
int month=0;
int day=0;
System.out.println("请输入年份");
Scanner sc = new Scanner(System.in);
try {
year = sc.nextInt();
} catch (Exception e) {
System.out.println("输入日期有误");
return;
}
System.out.println("请输入月份");
try {
month = sc.nextInt();
} catch (Exception e) {
System.out.println("输入日期有误");
return;
}
System.out.println("请输入日");
try {
day = sc.nextInt();
} catch (Exception e) {
System.out.println("输入日期有误1");
return;
}
if (year < 1900 || year > 2050){
System.out.println("输入日期无效");
return;
}
if (month < 1 || month > 12){
System.out.println("输入日期无效");
return;
}
if (day < 1 || day > 31){
System.out.println("输入日期无效");
return;
}
if (year % 4 == 0 && month == 2 && day > 29){
System.out.println("输入日期无效");
return;
}
if (year % 4 != 0 && month == 2 && day > 28){
System.out.println("输入日期无效");
return;
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10){
if (day >31){
System.out.println("输入日期无效");
return;
}
}
if (month == 4 || month == 6 || month == 9 || month == 11){
if (day > 30) {
System.out.println("输入日期无效");
return;
}
}
System.out.println(getWeekday(year,month,day));
}
}
赛哥的代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
String year = null;//年份
String month = null;//月份
String day = null;//日
//输入年月日
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
Scanner scanner3 = new Scanner(System.in);
System.out.println("请输入年份:1900-2050:");
year=scanner1.next();
//判断年份
try {
//强制转换成int类型
int year1= Integer.parseInt(year);
boolean result=PanduanYear(year1);//判断段年份
//System.out.println(year1);
if(result==false){
System.out.println("输入的年份出超出查询范围");
return;
}
}catch (Exception e){
System.out.println("年份输入有误,请输入正确的年份");
return;
}
//判断月份
System.out.println("请输入月份1-12:");
month=scanner2.next();
try {
//强制转换成int类型
int month1= Integer.parseInt(month);
boolean result=PanduanMonth(month1);
if(result==false){
System.out.println("输入的月份有误");
return;
}
}catch (Exception e){
System.out.println("月份输入有误,请输入正确的月份");
return;
}
//判断日期
System.out.println("请输入日:");
day=scanner3.next();
try {
//强制转换成int类型
int year1=Integer.parseInt(year);
int month1= Integer.parseInt(month);
int day1=Integer.parseInt(day);
boolean result=PanDay(year1,month1,day1);
if(result==false){
System.out.println("输入的日期有误");
return;
}
}catch (Exception e){
e.printStackTrace();
System.out.println("日期输入有误,请输入正确的日期");
return;
}
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy");
Date d = null;
try {
d = fmt.parse(day+" "+month+" "+year);
} catch (ParseException e) {
throw new RuntimeException(e);
}
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
switch(weekDay) {
case 1 : System.out.println("SUNDAY"); break;
case 2 : System.out.println("MONDAY"); break;
case 3 : System.out.println("TUESDAY"); break;
case 4 : System.out.println("WEDNESDAY"); break;
case 5 : System.out.println("THURSDAY"); break;
case 6 : System.out.println("FRIDAY"); break;
case 7 : System.out.println("SATURDAY"); break;
default: break;
}
}
//判断合理的年份
public static boolean PanduanYear (Integer year){
return year >= 1900 && year <= 2050;
}
//判断月份是否正常
public static boolean PanduanMonth (Integer month){
return month >= 1 && month <= 12;
}
//判断日期是否输入正常
public static boolean PanDay(Integer year,Integer month ,Integer day){
int run=0;//0是平年,1是闰年
int m=0;//0是31天,1是30天,2是29天,3是28天
boolean result = false;
if(year%400==0||year%4==0){
run=1;
}
if(month==4||month==6||month==9||month==11){
m=1;
}
if(month==2&&run==1){
m=2;
}
if (month==2&&run==0){
m=3;
}
if (m==1&&day>=1&&day<=30){
result=true;
}
if (m==0&&day>=1&&day<=31){
result=true;
}
if (m==2&&day>=1&&day<=29){
result=true;
}
if (m==3&&day>=1&&day<=28){
result=true;
}
return result;
}
}
标签:return,代码,System,month,println,软件测试,day,out From: https://www.cnblogs.com/zhaoshengfu/p/18088337