yyyymmddhhmmss java,Java在yyyyMMddHHmmss中验证日期

i want to validate the given date format as yyyyMMddHHmmss in java.

Conditions:

It should meet the format yyyyMMddHHmmss.

It should validate the current date.

It should validate hours which can be +3 hours or -3 hours variance with the current hour.

If all three conditions are met, the Java method should return true.

My current code only validates the yyyyMMddHHmmss format.

My current code

public class SO31132861 {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

df.setLenient(false);

System.out.println(tryParse(df, "20160630231110"));

System.out.println(tryParse(df, "20150228231100"));

System.out.println(tryParse(df, "20160229231100"));

System.out.println(tryParse(df, "21000229231100")); // 29th Feb on non-leap year 2100

System.out.println(tryParse(df, "20160631231110")); // 31st Jun invalid day

System.out.println(tryParse(df, "20160229231160")); // Second > 59

System.out.println(tryParse(df, "20150229231100")); // 29th Feb on non-leap year 2015

System.out.println(tryParse(df, "20150228241100")); // Hour > 23

}

private static Boolean tryParse(DateFormat df, String s) {

Boolean valid=false;

try {

Date d= df.parse(s);

valid=true;

} catch (ParseException e) {

valid=false;

}

return valid;

}

}

解决方案

Use two other dates, three hours ahead and three hours behind, for comparison. Then make sure the date that you are parsing is between those two boundaries using compareTo().

You really shouldn't put numbers in your class names, but that's a style issue that's irrelevant to the answer.

public class SO31132861 {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

df.setLenient(false);

System.out.println(tryParse(df, "20160630231110"));

System.out.println(tryParse(df, "20150228231100"));

System.out.println(tryParse(df, "20160229231100"));

System.out.println(tryParse(df, "21000229231100")); // 29th Feb on non-leap year 2100

System.out.println(tryParse(df, "20160631231110")); // 31st Jun invalid day

System.out.println(tryParse(df, "20160229231160")); // Second > 59

System.out.println(tryParse(df, "20150229231100")); // 29th Feb on non-leap year 2015

System.out.println(tryParse(df, "20150228241100")); // Hour > 23

}

private static Boolean tryParse(DateFormat df, String s) {

Boolean valid=false;

try {

Date threeHoursBefore = new Date();

threeHoursBefore.setTime(System.currentTimeMillis() - (3*60*60*1000));

Date threeHoursAfter = new Date();

threeHoursAfter.setTime(System.currentTimeMillis() + (3*60*60*1000));

Date dateToParse= df.parse(s);

valid=dateToParse.compareTo(threeHoursBefore) > 0 && dateToParse.compareTo(threeHoursAfter) < 0;

} catch (ParseException e) {

valid=false;

}

return valid;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值