validation - Java processing multiple possibly unknown date/time formats -


i trying create can check date/time of many formats see if valid date/time can process it. processing consists of converting date/time normalized software work with. problem have check in different ways try , detect date simpledateformat has been hard every variety. project related parsing csv files potentially user-specified date formats in date column. there better way? here doing right now:

public static boolean isdatevalid(string datestring) {     arraylist<simpledateformat> dateformats = new arraylist<simpledateformat>();      dateformats.add(new simpledateformat("m/dd/yyyy"));     dateformats.add(new simpledateformat("dd.m.yyyy"));     dateformats.add(new simpledateformat("m/dd/yyyy hh:mm:ss"));     dateformats.add(new simpledateformat("dd.m.yyyy hh:mm:ss"));     dateformats.add(new simpledateformat("dd-m-yyyy hh:mm:ss"));     dateformats.add(new simpledateformat("m-dd-yyyy hh:mm:ss"));     dateformats.add(new simpledateformat("yyyy-m-dd hh:mm:ss"));     dateformats.add(new simpledateformat("dd.mmm.yyyy"));     dateformats.add(new simpledateformat("dd-mmm-yyyy"));     dateformats.add(new simpledateformat("m/dd"));     dateformats.add(new simpledateformat("m dd"));     dateformats.add(new simpledateformat("m y"));     (simpledateformat format : dateformats)      {         try          {             format.setlenient(false);             date date = format.parse(datestring);             return true;         }          catch (exception e)          {          }     }     return false; } 

additionally have tried using dateutils apache commons seems still have cycle through bunch of dates.

public static boolean isdate(string datestring) {     try     {         date thedate = dateutils.parsedate(datestring);     }     catch (exception e)     {         system.out.println(e);         return false;     }     return true; } 

any ideas or words of encouragement?

one problem current approach unnecessarily creating of instances of simpledateformat when know going use one. instead, use regular expressions detect sort of date string receiving , 1 of 2 things: 1) instantiate formatter need, or 2) manipulate string in standard format can parse 1 common formatter.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -