Skip to content

Instantly share code, notes, and snippets.

@nestoru
Created December 5, 2014 17:12
Show Gist options
  • Select an option

  • Save nestoru/f8429ab539eca209dc77 to your computer and use it in GitHub Desktop.

Select an option

Save nestoru/f8429ab539eca209dc77 to your computer and use it in GitHub Desktop.
Custom Java Multiple Dates Parser
/**
*
* 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