【发布时间】:2018-11-23 22:17:49
【问题描述】:
在注册某人之前,我需要确认出生日期不大于之前的日期,例如(01/01/1970) 和当前日期。
对我不起作用的方法:
public void agregarPersona() throws Exception {
ImplPersonaD dao;
try {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String fechaActual = String.valueOf(dateFormat.format(date));
String fechaPasada = "01/01/1970";
if(fechaPasada > persona.getFechNac() <= fechaActual ){
dao = new ImplPersonaD();
dao.agregarPersona(persona);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Satisfactorio", "Ingresado correctamente"));
}
else{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Invalido", "Fecha invalida"));
}
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Hubo un problema"));
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow!寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误和重现它所需的最短代码在问题本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。
-
什么不起作用?是
String fechaActual= String.valueOf(Calendar.DATE/Calendar.MONTH/Calendar.YEAR);吗? -
不要使用
Calendar类。它存在设计问题并且已经过时了。使用java.time, the modern Java date and time API, 尤其是它的LocalDate类作为没有时间和时间的日期。查看它的isBefore或isAfter方法。 -
仅供参考,非常麻烦的旧日期时间类,例如
java.util.Date、java.util.Calendar和java.text.SimpleDateFormat现在是 legacy,被 Java 8 中内置的 java.time 类所取代,之后。见Tutorial by Oracle。
标签: java validation date project