Results 1 to 8 of 8
  1. #1
    mike78 is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default Finding date pattern in a LinkedList

    I'm new to Java and have bought a book to learn but there are no answers in it and I have come across a problem which I can't solve. I'm supposed to check whether the date entered by the user matches one of the 3 allowed forms (I can define them) and if not, warn the user that he has entered an invalid date.

    Java Code:
    public static boolean isAllowedDate(String date){
    List<DateFormat> allowedDate = new LinkedList<DateFormat>();
    allowedDate.add(new SimpleDateFormat("yyyy-MM-dd"));
    allowedDate.add(new SimpleDateFormat("yyyy.MM.dd"));
    allowedDate.add(new SimpleDateFormat("yyyy/MM/dd"));
    
    SimpleDateFormat myDate= new SimpleDateFormat();
    
    for (DateFormat myList: allowedDate) {
    try {
    myDate.parse(date);
    if (allowedDate.contains(myDate)){
    return true;
    }
    }
    catch (ParseException e) {
    JOptionPane.showMessageDialog(null, "Wrong date format", "Error!",JOptionPane.ERROR_MESSAGE);
    return false;
    }
    }
    return true;		
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mike78 View Post
    I'm new to Java and have bought a book to learn but there are no answers in it and I have come across a problem which I can't solve. I'm supposed to check whether the date entered by the user matches one of the 3 allowed forms (I can define them) and if not, warn the user that he has entered an invalid date.

    Java Code:
    public static boolean isAllowedDate(String date){
    List<DateFormat> allowedDate = new LinkedList<DateFormat>();
    allowedDate.add(new SimpleDateFormat("yyyy-MM-dd"));
    allowedDate.add(new SimpleDateFormat("yyyy.MM.dd"));
    allowedDate.add(new SimpleDateFormat("yyyy/MM/dd"));
    
    SimpleDateFormat myDate= new SimpleDateFormat();
    
    for (DateFormat myList: allowedDate) {
    try {
    myDate.parse(date);
    if (allowedDate.contains(myDate)){
    return true;
    }
    }
    catch (ParseException e) {
    JOptionPane.showMessageDialog(null, "Wrong date format", "Error!",JOptionPane.ERROR_MESSAGE);
    return false;
    }
    }
    return true;		
    }
    Shouldn't you try all SimpleDateFormatters in your list attempt to parse a certain String representation? If one succeeds you want to return true; false otherwise; that loop becomes:

    Java Code:
    for (DateFormat myFormatter: allowedDate) { // try all the formatters
       try {
          if (myFormatter.parse(date)) != null) // did it succeed?
             return true;
       }
       catch (ParseException pe) { // no it didn't succeed, continue
       }
    }
    return false; // none managed to parse this date
    I don't know what that single myDate thing is doing in there ...

    kind regards,

    Jos

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Or a regex:
    Java Code:
    public static boolean isAllowedDate(String date){
       return date.matches("\\d{4}((-\\d{2}){2}|(\\.\\d{2}){2}|(/\\d{2}){2})");
    }
    I know the regex is ugly, but it should work.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    Or a regex:
    Java Code:
    public static boolean isAllowedDate(String date){
       return date.matches("\\d{4}((-\\d{2}){2}|(\\.\\d{2}){2}|(/\\d{2}){2})");
    }
    I know the regex is ugly, but it should work.
    That would accept dates like: 2010/42/54 while those DateFormatters barf on it.

    kind regards,

    Jos

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Ohh, silly me. Of course that won't work, I only thought about format not content.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    Ohh, silly me. Of course that won't work, I only thought about format not content.
    You're not a politician are you?

    kind regards,

    Jos ;-)

  7. #7
    mike78 is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Shouldn't you try all SimpleDateFormatters in your list attempt to parse a certain String representation? If one succeeds you want to return true; false otherwise; that loop becomes:

    Java Code:
    for (DateFormat myFormatter: allowedDate) { // try all the formatters
       try {
          if (myFormatter.parse(date)) != null) // did it succeed?
             return true;
       }
       catch (ParseException pe) { // no it didn't succeed, continue
       }
    }
    return false; // none managed to parse this date
    I don't know what that single myDate thing is doing in there ...

    kind regards,

    Jos
    Thank you for your answer. Your solution works. However, when I enter a date like "2000/12/12" I get two error messages because this string doesn't match the method's first two date forms. So a warning will be shown for all the lines before the searched form will appear in my list. Assuming I had a list with 10 different possible date formats and I entered something like "12.12.2000a" and this letter "a" by mistake I would get 10 error messages. Is there any way to show the warning message only once?

    This whole programming stuff is really interesting for a beginner but there are so many situations when it takes so much time to find out how things should be done correctly...But I don't intend to give up.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mike78 View Post
    Thank you for your answer. Your solution works. However, when I enter a date like "2000/12/12" I get two error messages because this string doesn't match the method's first two date forms. So a warning will be shown for all the lines before the searched form will appear in my list. Assuming I had a list with 10 different possible date formats and I entered something like "12.12.2000a" and this letter "a" by mistake I would get 10 error messages. Is there any way to show the warning message only once?

    This whole programming stuff is really interesting for a beginner but there are so many situations when it takes so much time to find out how things should be done correctly...But I don't intend to give up.
    It's your program that is showing the errors isn't it? Everytime a formatter fails you're showing the error, right? The remedy is simple: add a boolean variable, say showError and set it to true if an error is supposed to be shown and set it to false if no more errors are supposed to be shown. Check the value of that variable just before your program is about to show an error.

    kind regards,

    Jos

Similar Threads

  1. Default/System Date Format Pattern?
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-05-2008, 08:34 AM
  2. How to format the date in particular pattern
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-04-2008, 03:35 PM
  3. How to format the date in particular pattern
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •