in Education by
I am getting a null pointer exception when a user enters date in wrong format. Method to convert String to Date Date stringToDate(String dateString) { Date returnDate = null; if (dateString!= null && dateString.length() > 0 && isValidDate(dateString)) { returnDate = dateFormat.parse(dateStr); } return returnDate; } and boolean isValidDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); Pattern datePattern = Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{4}"); Matcher datePatternMatch = datePattern.matcher(date); boolean datePatternMatchfound = datePatternMatch.matches(); if(date==null){ return false; } else if(date!=null && date.length()>0){ if(datePatternMatchfound){ sdf.setLenient(false); sdf.parse(date.trim()); } return true; } else { return false; } } I am just curious to know .... 1) what should be valid pattern for date? 2) if the user enters wrong date stringToDate method will certainly get failed and throw a null pointer exception. How to avoid that? Any help would really be appreciated. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
you are assuming the SimpleDateFormat(MM-dd-yyyyas the default pattern the user will input, either you should make sure your user can only enter in SimpleDateFormat, or you should make changes in isValidDate() to accept

Related questions

0 votes
    I am trying to convert a string into date format in Google Sheet. I've tried different formulas but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm reading text from a flat file in c# and need to test whether certain values are dates. They ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm reading text from a flat file in c# and need to test whether certain values are dates. They ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm reading text from a flat file in c# and need to test whether certain values are dates. They ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm reading text from a flat file in c# and need to test whether certain values are dates. They ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm reading text from a flat file in c# and need to test whether certain values are dates. They ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    How to convert a string to a date in Oracle database?...
asked Dec 18, 2020 in Technology by JackTerrance
0 votes
    This question already has answers here: converting date format (5 answers) Closed 5 years ago. I have a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Is there a built-in method for converting a date to a datetime in Python, for example getting the datetime for the ... .month, d.day)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How can I convert Persian date to Gregorian date using System.globalization.PersianCalendar? Please note that I ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    How to convert a date to char in Oracle? Give one example....
asked Dec 18, 2020 in Technology by JackTerrance
0 votes
    I want' to parse dates in this format, but ignore parts of the string. 'Wed, 27 Oct 1770 22:17: ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Me and my colleague have different versions of VisualStudio. He used interpolated string and I couldn't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
...