Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java program for reading in a date and validating it with proper calendar regex including leap year and months

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program for reading in a date and validating it with proper calendar regex including leap year and months

    Hello everyone:

    I have almost finished my java school assignment and am now going for the bonus task which is to (when I read in a date from the user) I must validate it not only in the correct format (DD-MM-YYY with the dashes inbetween) but I have to validate it according to a proper calendar including leap years and some months only having 30 days instead of others having 31. I have figured out a way of doing this and I will call the isLeapYear() function but what I must first to is break this:

    20-10-2009 into individual day month year int values.

    NOW the only problem that I am going to have is "cutting" the string that I have into a day month year and validating that. What I mean is if I get a string from the user (and they type in 20-10-2009 ) how could I possibly break this up into the appropriate day month year so in turn I could get that year and check if it is a leap year?

    Unfortunately I HAVE to read in a string otherwise it is a "spec violation" for my assignment.

    Could anybody point me in the right direction so that I may figure out how to accomplish this?

    Thanks,

    Adam Scott


  2. #2
    Junior Member
    Join Date
    Feb 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in a date and validating it with proper calendar regex

    This is what I ended up doing...

     
    String VALID_DATE_REGEX = "(0[1-9]|[12][0-9]|3[01])[-](0[1-9]|1[012])[-](20\\d\\d)";
     
    Pattern DATE_PATTERN = Pattern.compile(VALID_DATE_REGEX);
     
    String actDate = "20-12-2020";
     
    Matcher mDateMatch = DATE_PATTERN.matcher( actDate );
     
    System.out.println("Date: "+mDateMatch.group(1));
    System.out.println("Month: "+mDateMatch.group(2));
    System.out.println("Date: "+mDateMatch.group(3));

    Output:

    Date: 20
    Month: 12
    Year: 2020

Similar Threads

  1. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 06:53 PM
  2. Updation of webpage to the last modified date
    By Revathy G in forum Java Programming Tutorials
    Replies: 1
    Last Post: May 6th, 2009, 05:47 AM
  3. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 09:14 AM
  4. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 04:51 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 10:31 AM