Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

date format

Options
  • 18-04-2006 11:07am
    #1
    Registered Users Posts: 1,305 ✭✭✭


    hi!

    i'm looking for java code to make sure a date is in the right format! i'm looking for the date to be entered in DD/MM/YYYY format need code to make sure that the entered date is a valid date in that format.

    Just wanna know does anyone no where i could find code to do this or does anyone have code to do this its really annoying me at this stage!
    thanks,
    J.


Comments

  • Closed Accounts Posts: 69 ✭✭Layla1981


    import java.text.SimpleDateFormat;
    import java.text.ParsePosition;
    import java.util.Date;

    public class DateFormatTest {
    public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    df.setLenient(false);
    ParsePosition pos = new ParsePosition(0);

    String strDate = " 12/67/200394";

    Date date = df.parse(strDate, pos);

    // Check all possible things that signal a parsing error
    if ((date == null) || (pos.getErrorIndex() != -1)) {
    System.out.println("Error: " + pos.getIndex());
    if (date == null) {
    System.out.println("Date is null");
    }
    if (pos.getErrorIndex() != -1) {
    System.out.println("Error index: " + pos.getErrorIndex());
    }
    }

    }
    }
    i hope there is something there that helps


  • Closed Accounts Posts: 69 ✭✭Layla1981


    import java.text.SimpleDateFormat;
    import java.text.ParsePosition;
    import java.util.Date;

    public class DateFormatTest {
    public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    df.setLenient(false);
    ParsePosition pos = new ParsePosition(0);

    String strDate = " 12/67/200394";

    Date date = df.parse(strDate, pos);

    // Check all possible things that signal a parsing error
    if ((date == null) || (pos.getErrorIndex() != -1)) {
    System.out.println("Error: " + pos.getIndex());
    if (date == null) {
    System.out.println("Date is null");
    }
    if (pos.getErrorIndex() != -1) {
    System.out.println("Error index: " + pos.getErrorIndex());
    }
    }

    }
    }
    i hope there is something there that helps


Advertisement