Created
December 5, 2014 17:12
-
-
Save nestoru/f8429ab539eca209dc77 to your computer and use it in GitHub Desktop.
Custom Java Multiple Dates Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * See it in action in http://runnable.com/VIHI8AhI-_MpHcsM/multipledatesparser-java | |
| * | |
| * */ | |
| class MultipleDatesParser { | |
| public static String stringToDate (String dateToParse) throws java.lang.Exception { | |
| String parsedDate = null; | |
| java.text.SimpleDateFormat sdf = null; | |
| try { | |
| sdf = new java.text.SimpleDateFormat("MM/DD/yy"); | |
| parsedDate = sdf.parse(dateToParse).toString(); | |
| } catch (Exception e1) { | |
| try { | |
| sdf = new java.text.SimpleDateFormat("MM/DD/yyyy"); | |
| parsedDate = sdf.parse(dateToParse).toString(); | |
| } catch (Exception e2) { | |
| //Other data formats here following the pattern above or ... | |
| throw e2; | |
| } | |
| } | |
| return parsedDate; | |
| } | |
| public static void main (String[] args) throws java.lang.Exception { | |
| System.out.println(stringToDate("12/20/2014")); | |
| System.out.println(stringToDate("12/20/14")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment