-1

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;
}
}
haja
  • 135
  • 1
  • 4
  • 12
  • 3
    Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://whathaveyoutried.com), and where you are stuck. This will also help us answer your question better. – Elliott Frisch Nov 13 '15 at 02:56
  • 1
    I'm seeing that you've posted several requirements, but I don't see any focused and specific questions just yet. What have you done? Where exactly are you stuck? Please have a look at the [help] for more information on how to improve your question and increase your chances of getting decent help. – Hovercraft Full Of Eels Nov 13 '15 at 02:58
  • The first two could be achieved with a `SimpleDateFormat`, the third would probably require maybe a `LocalDateTime` from either Java 8's Time API or JodaTime – MadProgrammer Nov 13 '15 at 03:01
  • You could also compare dates by `date.getTime()` on millisecond level (as long). But I'd also recommend trying it by yourself first. – mad_manny Nov 13 '15 at 06:52
  • 1
    It's not at all clear what you mean by "It should validate the current date" - do you mean the only valid date is the current date? It seems odd to include the date on the input, to be honest... – Jon Skeet Nov 13 '15 at 07:01
  • 1
    Additionally, you need to specify which time zone these validation rules are meant to work in. And if it's currently 1am, does the "3 hours before" rule mean that yesterday's date *is* valid after all? – Jon Skeet Nov 13 '15 at 07:02
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 13 '18 at 01:08

1 Answers1

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;
}
}
Brad
  • 2,261
  • 3
  • 22
  • 32