-1

I want to validate data entered by user in 'date and time' JTextField with (yyyy-MM-dd hh:mm:ss.SSS ) this format , please help me to get out of this, am not getting any idea.

How to validate entered data with this format (yyyy-MM-dd hh:mm:ss.SSS)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Srikanth Togara
  • 49
  • 2
  • 11
  • 1
    If you have no idea how to even start this, I doubt you're suited to programming. Perhaps go into a simpler job. *"any help is appreciated,"* Don't forget to ask if the customer would like fries with their order. – Andrew Thompson Sep 21 '16 at 08:50
  • 2
    Use a `JFormattedTextField` with a `SimpleDataFormat`, similar to what is done [in this answer](http://stackoverflow.com/a/13424140/1076463). – Robin Sep 21 '16 at 12:17

1 Answers1

1

You can validate like this, if exception you need to notify user that it is not a valid format

    try {
        Date date = null;
        String inputDate = "2016-01-07 10:11:30.500";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        dateFormat.setLenient(false);
        date = dateFormat.parse(inputDate);
        System.out.println(date);
    } catch (ParseException parseException) { 
        //Code should be added to handle invalid date format
    }
ravthiru
  • 8,878
  • 2
  • 43
  • 52