如何在Java中显示有效日期的数量?
我正在考虑创建一个方法,该方法应该返回有效日期的数目。如何使用SimpleDataFormat如何在Java中显示有效日期的数量?,java,string,algorithm,oop,Java,String,Algorithm,Oop,我正在考虑创建一个方法,该方法应该返回有效日期的数目。如何使用SimpleDataFormat class ValidatorDate { public static int ValidDates(String[] words) { DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); format.setLenient(false); } } public class Main { public stat
class ValidatorDate {
public static int ValidDates(String[] words) {
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setLenient(false);
}
}
public class Main {
public static void main(String[] args) {
System.out.println(ValidatorDate.ValidDates(new String[]{"car","bench","01/04/2019",
"01/13/2019", "29/02/200s"}));
// 2
}
}
您可以在
try/catch
块中解析每个日期及其数字,并按如下方式递增计数器:
public static void main(String[] args) {
String[] dates = new String[]{"car","bench","01/04/2019", "01/13/2019", "29/02/200s"};
System.out.println(validate(dates));
}
private static int validate(String[] dates){
int count = 0;
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setLenient(false);
for(String date : dates) {
try {
format.parse(date);
String[] dateParts = date.split("/");
for(String str : dateParts)
Integer.parseInt(str);
if(dateParts.length!=3 || dateParts[0].length()!=2 || dateParts[1].length()!=2 || dateParts[2].length()!=4)
throw new Exception();
count++;
} catch (Exception e) {
System.out.println("Date " + date + " is not valid");
}
}
return count;
}
输出:
Date car is not valid
Date bench is not valid
Date 01/13/2019 is not valid
Date 29/02/200s is not valid
1
编辑:
根据Ole的评论,最好使用更精确的库:
public static void main(String[] args) {
String[] dates = new String[]{"car","bench","01/04/2019", "01/13/2019", "29/02/200s"};
System.out.println(validate(dates));
}
private static int validate(String[] dates){
int count = 0;
for(String date : dates) {
try {
String[] dateParts = date.split("/");
if(dateParts.length==3 && isDateValid(Integer.parseInt(dateParts[2]), Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[0])))
count++;
else
throw new Exception();
} catch (Exception e) {
System.out.println("Date " + date + " is not valid");
}
}
return count;
}
private static boolean isDateValid(int year, int month, int day) {
boolean dateIsValid = true;
try {
LocalDate.of(year, month, day);
} catch (DateTimeException e) {
dateIsValid = false;
}
return dateIsValid;
}
在您的方法中:
returnarrays.stream(words).filter(e->{//此处验证-'e'是数组中的当前单词}).mapToInt(e->1.sum()代码>我不能使用流。我建议您不要使用SimpleDateFormat
和Date
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。取而代之的是使用LocalDate
和DateTimeFormatter
,两者都来自。@GeorgeChetan29/02/200s
无效,因为它在“年份部分”中没有4位,并且SimpleDateFormat
应为4位digits@GeorgeChetan哎呀,我错了,是Integer.parseInt(str)
出现异常:java.lang.NumberFormatException:对于输入字符串:“200s”
,日期是有效的,并且SimpleDataFormat
接受它,但此validate()方法将其视为invalid@MarcoLucidi这就是我添加for循环的原因。为什么图书馆认为200s是有效年?根据你的方法1/2/3/4
是有效日期。事实并非如此。我不知道OP是否接受29/02/200
(没有s
),但您的代码接受。@MajedBadawi the说“该方法可能不会使用给定字符串的整个文本”。我认为一旦它看到一个无效的字符,它就会停止,如果它能够使用它已经解析的内容返回一个有效的日期,它就会返回给您。我不知道为什么即使在.setLenient(false)
之后它也会以这种方式工作。