我知道我可以使用日期格式化程序,但我正在尝试学习。
我正在尝试以Scanner
格式mm/dd/yyyy
开始约会。当我接受本月的输入时,我遇到了麻烦。例如,如果我想要1月份,请按01,monthInt
将其转换为1
而不是01
。
我也遇到了麻烦。当我输入有效日期时,它一直说无效的一天。我的if
声明搞砸了吗?如果您需要更多解释,请询问。谢谢你的时间!
我的代码:
System.out.println("Enter a date in the format mm/dd/yyyy");
String mm;
String dd;
String yyyy;
String January = null;
String February = null;
String March = null;
String April = null;
String May = null;
String June = null;
String July = null;
String August = null;
String September = null;
String October = null;
String November = null;
String December = null;
String month = null;
String date = null;
Scanner keyboard = new Scanner(System.in);
date = keyboard.next();
if (date.length() != 10) {
System.out.println("Format of date is wrong");
}
if (date.charAt(2) != '/') {
System.out.println("Must have / after mm");
}
if (date.charAt(5) != '/') {
System.out.println("Must have / after dd");
}
else if (date.length() == 10 && date.charAt(2) != '/' && date.charAt(5) != '/') {
System.out.println(date + " date is valid");
}
mm = date.substring(0, 1);
int monthInt = Integer.parseInt(mm);
if (monthInt == 1) {
month = January;
} else if (monthInt == 2) {
month = February;
} else if (monthInt == 3) {
month = March;
} else if (monthInt == 4) {
month = April;
} else if (monthInt == 5) {
month = May;
} else if (monthInt == 6) {
month = June;
} else if (monthInt == 7) {
month = July;
} else if (monthInt == 8) {
month = August;
} else if (monthInt == 9) {
month = September;
} else if (monthInt == 10) {
month = October;
} else if (monthInt == 11) {
month = November;
} else if (monthInt == 12) {
month = December;
} else {
System.out.println("Invalid Month");
}
if (monthInt < 0 || monthInt > 12) {
System.out.println("Invalid month");
}
dd = date.substring(3, 4);
int dayInt = Integer.parseInt(dd);
if (monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10
|| monthInt == 12 && dayInt < 0 && dayInt > 31) {
System.out.println("Invalid day");
}
if (monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11
&& dayInt < 0 && dayInt > 30) {
System.out.println("Invalid day");
}
if (monthInt == 2 && dayInt < 0 && dayInt > 28){
System.out.println("Invalid day");
}
答案 0 :(得分:3)
您在无效日检查中的if语句不正确。它应该是这样的:
if ((monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10 || monthInt == 12) && (dayInt <= 0 || dayInt > 31)) {
System.out.println("Invalid day");
}
if ((monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11) && (dayInt <= 0 || dayInt > 30)) {
System.out.println("Invalid day");
}
if (monthInt == 2 && (dayInt <= 0 || dayInt > 28)) {
System.out.println("Invalid day");
}
请注意,dayInt < 0
语句已更改为dayInt <=0
,因为0的日期int也无效。
这里的关键是,如果它是给定月份之一,您希望它是无效日,并且它超出该月的日期范围。因此,您使用if(month1 OR month2 etc) AND (outsiderange1 OR outsiderange2)
形式的复合词。
答案 1 :(得分:0)
看起来问题就在这里:
mm = date.substring(0, 2);
它取日期字符串中的第一个字符,因此如果您输入'01'表示1月,它将为'0',因此monthInt将为0,您将收到“无效月份”错误。如果您想获得两个字符,则需要dd = date.substring(3, 5);
日期存在同样的问题,您需要df5.set_index(['Date Time'], inplace=True)