Calendar Class rollback

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hostel
    New Member
    • Feb 2007
    • 17

    #1

    Calendar Class rollback

    when we use the calendar class and enter the date in date object as 45/8/2007(dd/mm/yyyy). it will automatically roll forward as 15/9/2007(dd/mm/yyyy)

    Logically this should not happen as date cannot be greater than 31 ,

    How to avoid roll back and i also need help in validating date

    thanks in advance
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by hostel
    when we use the calendar class and enter the date in date object as 45/8/2007(dd/mm/yyyy). it will automatically roll forward as 15/9/2007(dd/mm/yyyy)

    Logically this should not happen as date cannot be greater than 31 ,

    How to avoid roll back and i also need help in validating date

    thanks in advance

    Then I think it can not be done using Calendar Class.
    Then you should have your own Class.
    Wait sometimes let the experts say.
    Then proceed with your own class.

    Debasis Jana

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Have you looked at the API? You would have found the method setLenient in both DateFormat and Calendar:

      DateFormat.setL enient
      Calendar.setLen ient

      Your note made it sound like you are not using DateFormat, but you should use it to parse dates. Here is a demo showing the effect of setLenient:

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

      public class DateParsingExam ple {
      public static void main(String[] args) {
      String data = "45/8/2007";
      DateFormat df = new SimpleDateForma t("dd/MM/yyyy");
      boolean[] lenientSettings = {true, false};

      for (boolean lenient : lenientSettings ) {
      try {
      df.setLenient(l enient);
      System.out.prin tln("lenient = " + lenient);
      Date date = df.parse(data);
      System.out.prin tln(date);
      } catch (ParseException e) {
      e.printStackTra ce();
      }
      }
      }
      }[/CODE]

      Comment

      Working...