我想知道Java中是否有任何方法可以做到这一点.否则我可能会使用Regex解决方案.
我有来自用户的输入字符串,可以是任何字符.我想检查输入字符串是否符合我所需的日期格式.
因为我输入20130925并且我所需的格式是dd/MM/yyyy所以,对于这种情况我应该是假的.
我不想转换这个日期我只想检查输入字符串是否符合所需的日期格式.
我试过以下
Date date = null;
try {
date = new SimpleDateFormat("dd/MM/yyyy").parse("20130925");
} catch (Exception ex) {
// do something for invalid dateformat
}
Run Code Online (Sandbox Code Playgroud)
但我的catch(Exception ex)块无法捕获SimpleDateFormat.Parse()生成的任何异常;
Mad*_*mer 98
以未知格式解析字符串的日期/时间值本质上是不可能的(让我们面对它,3/3/3实际意味着什么?!),我们所能做的就是"尽力而为"
这个解决方案不抛出Exception,它返回一个boolean,这是设计的.任何Exceptions都纯粹用作防护机制.
因为它现在是2018年,Java 8+具有日期/时间API(其余的具有ThreeTen反向端口).解决方案基本保持不变,但稍微复杂一些,因为我们需要执行检查:
这使它看起来像......
public static boolean isValidFormat(String format, String value, Locale locale) {
LocalDateTime ldt = null;
DateTimeFormatter fomatter = DateTimeFormatter.ofPattern(format, locale);
try {
ldt = LocalDateTime.parse(value, fomatter);
String result = ldt.format(fomatter);
return result.equals(value);
} catch (DateTimeParseException e) {
try {
LocalDate ld = LocalDate.parse(value, fomatter);
String result = ld.format(fomatter);
return result.equals(value);
} catch (DateTimeParseException exp) {
try {
LocalTime lt = LocalTime.parse(value, fomatter);
String result = lt.format(fomatter);
return result.equals(value);
} catch (DateTimeParseException e2) {
// Debugging purposes
//e2.printStackTrace();
}
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这使得以下......
System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925", Locale.ENGLISH));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013", Locale.ENGLISH));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013 12:13:50", Locale.ENGLISH));
System.out.println("isValid - yyyy-MM-dd with 2017-18--15 = " + isValidFormat("yyyy-MM-dd", "2017-18--15", Locale.ENGLISH));
Run Code Online (Sandbox Code Playgroud)
输出...
isValid - dd/MM/yyyy with 20130925 = false
isValid - dd/MM/yyyy with 25/09/2013 = true
isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false
isValid - yyyy-MM-dd with 2017-18--15 = false
Run Code Online (Sandbox Code Playgroud)
简单的尝试并使用类似的东西解析String所需的DateSimpleDateFormat
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(value);
if (!value.equals(sdf.format(date))) {
date = null;
}
} catch (ParseException ex) {
ex.printStackTrace();
}
if (date == null) {
// Invalid date format
} else {
// Valid date format
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以简单地编写一个执行此操作的简单方法,并true在Date非空时返回...
作为建议......
更新了运行示例
我不确定你在做什么,但是,下面的例子......
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateParser {
public static void main(String[] args) {
System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925"));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013"));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013 12:13:50"));
}
public static boolean isValidFormat(String format, String value) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(value);
if (!value.equals(sdf.format(date))) {
date = null;
}
} catch (ParseException ex) {
ex.printStackTrace();
}
return date != null;
}
}
Run Code Online (Sandbox Code Playgroud)
输出(类似的东西)......
java.text.ParseException: Unparseable date: "20130925"
isValid - dd/MM/yyyy with 20130925 = false
isValid - dd/MM/yyyy with 25/09/2013 = true
isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false
at java.text.DateFormat.parse(DateFormat.java:366)
at javaapplication373.JavaApplication373.isValidFormat(JavaApplication373.java:28)
at javaapplication373.JavaApplication373.main(JavaApplication373.java:19)
Run Code Online (Sandbox Code Playgroud)
不正确.对于isValidFormat("yyyy-MM-dd","2017-18--15"); 不抛出任何异常.
isValid - yyyy-MM-dd", "2017-18--15 = false
Run Code Online (Sandbox Code Playgroud)
似乎按我的预期工作 - 该方法不依赖于(也不抛出)异常来执行它的操作
Bab*_*aby 29
对于您的情况,您可以使用正则表达式:
boolean checkFormat;
if (input.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})"))
checkFormat=true;
else
checkFormat=false;
Run Code Online (Sandbox Code Playgroud)
如果您需要更灵活的解决方案,请参阅MadProgrammer的答案.
在发布这个答案差不多5年后,我意识到这是一种验证日期格式的愚蠢方法.但我会在这里留下这个告诉人们使用正则表达式验证日期是不可接受的
小智 17
您可以尝试使用简单的日期格式valdation
public Date validateDateFormat(String dateToValdate) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HHmmss");
//To make strict date format validation
formatter.setLenient(false);
Date parsedDate = null;
try {
parsedDate = formatter.parse(dateToValdate);
System.out.println("++validated DATE TIME ++"+formatter.format(parsedDate));
} catch (ParseException e) {
//Handle exception
}
return parsedDate;
}
Run Code Online (Sandbox Code Playgroud)
tiw*_*ash 10
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
formatter.setLenient(false);
try {
Date date= formatter.parse("02/03/2010");
} catch (ParseException e) {
//If input date is in different format or invalid.
}
Run Code Online (Sandbox Code Playgroud)
formatter.setLenient(false)将强制执行严格匹配.
如果你正在使用Joda-Time -
private boolean isValidDate(String dateOfBirth) {
boolean valid = true;
try {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dob = formatter.parseDateTime(dateOfBirth);
} catch (Exception e) {
valid = false;
}
return valid;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
234036 次 |
| 最近记录: |