calender validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugard
    New Member
    • Aug 2007
    • 50

    calender validation

    I am having a problem in finding out whether a date inserted by the user happens to be before the current todays date.

    The problem is this.. The date that the user inputs is in the format of simpledateforma t dd-MM-yyyy. Therefore i cannot use the date.before() since simple date format is of type calender. Also the date that the user is going to insert is going to be placed in a text field. Therefore i need to get it from there and compare it to the current's date. I tried to use the calender.getIns tance(), though i did not suceed.

    If someone can help me, it would be really appreciated. Thanks
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Demo:

    [CODE=Java]import java.text.*;
    import java.util.*;

    public class CalendarExample {
    public static void main(String[] args) throws ParseException {
    Calendar c = Calendar.getIns tance();
    Date now = c.getTime();
    DateFormat df = new SimpleDateForma t("yyyy-MM-dd");
    Date then = df.parse("2008-01-02");
    System.out.prin tln(then.before (now));
    }
    }[/CODE]
    To be more careful, I should have moved the current time back to midnight.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      If I had been picky, I would have posted this ;-)

      Originally posted by sugard
      The problem is this.. The date that the user inputs is in the format of simpledateforma t dd-MM-yyyy. Therefore i cannot use the date.before() since simple date format is of type calender.
      Huh? java.text.Simpl eDateFormat parses strings into Date objects, so you can use before.

      Originally posted by sugard
      I tried to use the calender.getIns tance(), though i did not succeed.
      Insufficient data for a meaningful response.

      Comment

      Working...