Speed up development with full-stack environments for every branch.

Learn More

MultipleDatesParser.java

Forked from Basic Java Example.

42 Runs 62 Views 7 Copies
Saved

Saved

nestor.urquiza 2

nestor.urquiza
published 2 years ago

    class MultipleDatesParser {
      public static String parse (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(parse("12/20/2014"));
        System.out.println(parse("12/20/14"));
      }
    }
    
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).