Check the Date Format.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #1

    Check the Date Format.

    Now a days I take care of sensitive coding rather than completing the code anyhow.
    So have a look at this code.
    I am trying to check the Date Format.
    I am here assuming that the date separator string is either "-" or "/".
    First I thought to do it using Regular Expression, but I am new to Regular Expression.
    So experts please do some comments here to improve the sensitivity of this code.

    [code=java]
    Date checkDate(Strin g date)throws Exception {
    Date temp_d;
    try{
    temp_d = d.parse(date);
    StringTokenizer st = new StringTokenizer (date,"-");
    int m = Integer.parseIn t(st.nextToken( ))-1,
    d = Integer.parseIn t(st.nextToken( )),
    y = Integer.parseIn t(st.nextToken( ));
    c.setTime(temp_ d);
    if(m!=c.get(Cal endar.MONTH) || d!=c.get(Calend ar.DATE) || y!=c.get(Calend ar.YEAR)) throw new Exception("Wron g date format");
    return temp_d;
    }catch(Exceptio n e)
    {
    throw e;
    }
    }
    [/code]

    Please do some comments on it.
    Is anything there to improve the logic :-)

    Kind regards,
    Dmjpro.
  • Ozzi
    New Member
    • Oct 2007
    • 4

    #2
    I would rather use SimpleDateForma t. Here is a very simple example of use.

    [CODE=java] //example of String to test
    String myDate = "02.03.2007 ";

    Date parsed = null;
    boolean formatOK = false;

    //first correct format
    SimpleDateForma t format1 =
    new SimpleDateForma t("dd/MM/yyyy");

    //second correct format
    SimpleDateForma t format2 =
    new SimpleDateForma t("dd.MM.yyyy") ;

    //if an unarseable exception occurs, we have to try the second format
    try {
    parsed = format1.parse(m yDate);
    formatOK = true;
    }catch (Exception e) {
    //do whatever
    }

    //if the String doesn't follows the second format
    if(!formatOK){
    try {
    parsed = format2.parse(m yDate);
    formatOK = true;
    } catch (ParseException e1) {
    //do whatever
    }

    }

    System.out.prin tln(formatOK?"f ormat OK":"format KO");[/CODE]
    Last edited by r035198x; Oct 4 '07, 10:21 AM. Reason: added code tags

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Ooops!
      Sorry for some code missing :-)
      The appropriate code is ........... "c" and "d" defined as

      [code=java]
      DateFormat d = new SimpleDateForma t("MM-dd-yyyy");
      Calendar c = new GregorianCalend ar();
      [/code]

      Sorry ... Now it's Ok !
      Please do some comments on it.

      Kind regards,
      Dmjpro.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by Ozzi
        I would rather use SimpleDateForma t. Here is a very simple example of use.

        //example of String to test
        String myDate = "02.03.2007 ";

        Date parsed = null;
        boolean formatOK = false;

        //first correct format
        SimpleDateForma t format1 =
        new SimpleDateForma t("dd/MM/yyyy");

        //second correct format
        SimpleDateForma t format2 =
        new SimpleDateForma t("dd.MM.yyyy") ;

        //if an unarseable exception occurs, we have to try the second format
        try {
        parsed = format1.parse(m yDate);
        formatOK = true;
        }catch (Exception e) {
        //do whatever
        }

        //if the String doesn't follows the second format
        if(!formatOK){
        try {
        parsed = format2.parse(m yDate);
        formatOK = true;
        } catch (ParseException e1) {
        //do whatever
        }

        }

        System.out.prin tln(formatOK?"f ormat OK":"format KO");
        I appreciate your coding :-)
        But please, try to use Code tags.
        Please read posting guidelines before Post.

        Debasis Jana

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by dmjpro
          I appreciate your coding :-)
          But please, try to use Code tags.
          Please read posting guidelines before Post.

          Debasis Jana
          Hey please do comments :-)

          Debasis Jana

          Comment

          Working...