Java Unparseable Date difference in formats -


how differentiate between data-entry being (a) invalid date or (b) invalid format?

i have following code handling date inputs text file.

    public boolean dateisvalid(string date) {         dateformat formatter = new simpledateformat("mm/dd/yyyy");                   formatter.setlenient(false);             try {                 date dateparsed = (date) formatter.parse(date);             } catch (parseexception e) {                 e.printstacktrace();             }             return false;          } 

i have working want to. problem have unable differentiate different parse exceptions thrown. example:

if string date = 18/10/2012 --> java.text.parseexception: unparseable date: "18/10/2012"
if string date = 2-12-2001 --> java.text.parseexception: unparseable date: "2-12-2001"

as can see, both wrong formats throw same error. how can differentiate them can handle them differently?

edit

to more precise, in case of date 18/10/2012, should throw invalid date error , in case of date 2-12-2001, need throw invalid format exception. dont need handle different formats. need way of getting different exceptions these 2 different cases.

the issue seems @ line

dateformat formatter = new simpledateformat("mm/dd/yyyy");   

for first error looks date coming first , month later should like

dateformat formatter = new simpledateformat("dd/mm/yyyy");  

second error shows incorrect format of date supplied since containing - whereas expecting format containing / ie like

dateformat formatter = new simpledateformat("dd-mm-yyyy"); 

if want handle different formats try this:

string[] formatdates= {"mm/dd/yyyy", "dd/mm/yyyy", "mm-dd-yyyy","dd-mm-yyyy"};  date tryparse(string datestring) {     (string formatdate: formatdates)     {         try         {             return new simpledateformat(formatdate).parse(datestring);         }         catch (parseexception e) {}     }      return null; } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -