0

I need to Validate Date in a specified format where both the inputs will be given only in the runtime in JTextfield and will be changing dynamically. Below is the code I have tried:

 Date dd = new Date();
    DateFormat df = new SimpleDateFormat(Date_format_text.getText());
    try {

        df.setLenient(false);
        Date d1 = df.parse(Lower_date_text.getText());
        System.out.println("Correct");
        validator_LD.setVisible(false);

    } catch (ParseException p) {

        validator_LD.setText("*Not in mentioned Format '" + df.format(dd) + "'");
        validator_LD.setVisible(true);

        System.out.println("Wrong");

    }

The above is.. i get the Date specified and the format specified from the text field and try to parse according to the specified format. If it doesn't match it will throw exception.

But this is not working properly in some cases :

  • If I give the Date 02/01/20'and the Format - dd/MM/YYYY where it should throw an exception since I have given the year as 20 and the format is 'YYYY' but i doesn't give exception.

Kindly help me.. Thanks in advance

Alex Michael Raj
  • 185
  • 2
  • 9
  • 24
  • Have a look at this link it will save you from a lot of trouble http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html – Jabir Mar 04 '14 at 04:54
  • 1
    `dates = new Date();` is a date object and your using it in `simple_format.format(dates);` so it will not give any exception and it work properly. – eatSleepCode Mar 04 '14 at 05:02
  • Sorry this is the correct code.. earlier one was to check the format.. `DateFormat df = new SimpleDateFormat(Date_format_text.getText()); df.setLenient(false); Date d1 = df.parse(Lower_date_text.getText());` //in the above.. I am trying to parse the Text to date so wen its not in the specified format it will gv exception but it does work for one purpose as i mentioned in the ques `validator_LD.setVisible(false); } catch (ParseException p) { System.out.println("Wrong"); }` – Alex Michael Raj Mar 04 '14 at 05:24

1 Answers1

2

First, you may want to take a look at How to Use the Focus Subsystem, paying attention to Validating Input which might help.

Second, as pointed out by @eatSleepCode, you're not actually parsing the text of the field, but are simply formatting an existing Date, so it will never throw an exception...

simple_format = new SimpleDateFormat(Date_format_text.getText());
// This is simply formatting the dates...
String ss = simple_format.format(dates);

Instead, you need to use something more like...

String test = "02/01/20";
String format = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setLenient(false);
try {
    Date date = sdf.parse(test);
    if (!sdf.format(date).equals(test)) {
        throw new ParseException(test + " is not a valid format for " + format, 0);
    }
} catch (ParseException ex) {
    ex.printStackTrace();
}

What this does, is test's the parser capabilities of the formatter, but also checks the input against what the resulting parsed Date would be formatted to, if these don't match it throws a ParseException. This is the closes I've been able to get to a strict parser...

Also, YYYY used to represent the week in year, not the year...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366