Java validate date in yyyyMMddHHmmss

落花浮王杯 提交于 2019-12-02 22:44:42

问题


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

Conditions:

  1. It should meet the format yyyyMMddHHmmss.

  2. It should validate the current date.

  3. 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;
}
}

回答1:


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;
}
}


来源:https://stackoverflow.com/questions/33685195/java-validate-date-in-yyyymmddhhmmss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!