Java中的日期零验证
我有一个方法,将Java中的日期零验证,java,date,Java,Date,我有一个方法,将日期字段作为输入参数 public static String formatDate(Date inputDate) { // if user send the date with inputDate= new Date(00000000) or new Date(0L) because it is Java default date. I have to send the exception error with message Invalid date. }
日期字段
作为输入参数
public static String formatDate(Date inputDate) {
// if user send the date with inputDate= new Date(00000000) or new Date(0L) because it is Java default date.
I have to send the exception error with message Invalid date.
}
public static String formatDate(Date inputDate) {
if (null == inputDate)
throw new FieldFormatException("Empty date field.");
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
return formatter.format(inputDate);
} catch (Exception ex) {
throw new FieldFormatException("Exception in formatting date field." + ex);
}
}
我所做的事情如下所示,但我无法在从历元传递无效的零计数日期(如“new date(0L)”作为inputDate参数时获得错误
public static String formatDate(Date inputDate) {
// if user send the date with inputDate= new Date(00000000) or new Date(0L) because it is Java default date.
I have to send the exception error with message Invalid date.
}
public static String formatDate(Date inputDate) {
if (null == inputDate)
throw new FieldFormatException("Empty date field.");
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
return formatter.format(inputDate);
} catch (Exception ex) {
throw new FieldFormatException("Exception in formatting date field." + ex);
}
}
听起来你只是想:
if (inputDate == null || inputDate.getTime() == 0L)
它将检测inputDate
是否为null或表示Unix纪元
但正如评论中所指出的:
- 拒绝单个值有点危险-为什么
是“错误的”,而新日期(0)
是“正确的”新日期(1)
- 这会阻止您接受恰好是Unix时代的合法输入
- 双向飞碟机接受的答案是正确的
tl;博士
java.time
您使用的是麻烦的旧日期时间类,现在已被java.time类取代
Instant
类取代了Date
作为UTC时间轴上的一个时刻,但分辨率更高,为纳秒,而不是毫秒
您可以在与尚未更新为java.time类的遗留代码交互时进行转换。若要转换,请调用添加到旧类中的新方法
Instant instant = myDate.toInstant() ;
检查空值
if ( null == input ) {
throw new IllegalArgumentException( "Received invalid input: null." ) ;
}
检查历元值0的计数,这似乎是您特别关心的。Instant
类有一个常数,从Unix和Java使用的历元参考日期时间开始计算零纳秒:UTC中1970年的第一个时刻
if ( input.equals( Instant.EPOCH ) ) {
throw new IllegalArgumentException( "Received invalid input: Instant.EPOCH. Input must be later than 1970-01-01T00:00:00Z." ) ;
}
如果业务规则要求,您可能需要检查最近的日期时间值
if ( input.isBefore( Instant.now() ) ) {
throw new IllegalArgumentException( "Received invalid input. Input must not be in the past." ) ;
}
生成字符串时,请考虑时区。对于任何给定的时刻,一天中的日期和时间在全球各地因区域而异
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
您想要的格式恰好是标准ISO 8601格式的“基本”版本。此格式预定义为常量:
“如果用户用inputDate=00000000发送日期”毫无意义,因为
inputDate
是一个date
,而不是String
。我的意思是新日期(0L)
或8个长度为0的值,我需要的是无效的日期,而不是转换为19700101
。对,这非常非常不清楚。为什么不直接调用Date.getTime()
,看看它是否返回0?另外,代码中注释中的“which is yyyyMMdd format”非常不清楚。Date
值没有格式。调用newdate(0L)
或Date(00000000L)
是同一件事-您将数字传递到-这意味着您正在创建一个日期,该日期相当于自历元起的零毫秒(1970-01-01T00:00Z
),即历元本身。你到底想做什么?我觉得值得一提的是,这有一种只检查一个日期值的可怕气味,只需新日期(1)
就可以轻松绕过。当我从一个开发人员那里收到这样的问题时,我通常不得不回去问“为什么你只关心那一个值?”而答案几乎总是他们关心的不仅仅是那一个值。如果有些人输入的是确切的纪元日期,会发生什么情况呢1970-01-01T00:00Z
?我将把getTime()
作为零,我有丢失数据的风险。@RameshKC:我满足了你的要求。如果你的要求不合理,你需要对此提出质疑。你想明确地拒绝1970-01-01T00:00Z——你不能两全其美。顺便说一句,我同意gnomed的担忧。我想知道的是,是否有一些系统的默认日期为零。在验证时,我必须放弃这些日期为零,如果日期不为零,这就是我试图实现的目标,并在yyyyMMdd中格式化。@RameshKC:对不起,我恐怕这个评论对我来说毫无意义。。。从根本上说,仅仅因为一个值恰好是默认值,并不意味着它也不能是真实数据。