Results 1 to 8 of 8
- 06-03-2010, 02:10 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
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; }
- 06-03-2010, 03:04 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
I don't know what that single myDate thing is doing in there ...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
kind regards,
Jos
- 06-03-2010, 03:17 PM #3
Or a regex:
I know the regex is ugly, but it should work.Java Code:public static boolean isAllowedDate(String date){ return date.matches("\\d{4}((-\\d{2}){2}|(\\.\\d{2}){2}|(/\\d{2}){2})"); }Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-03-2010, 03:28 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 06-03-2010, 03:44 PM #5
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
- 06-03-2010, 03:51 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 06-03-2010, 04:28 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
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.
- 06-03-2010, 04:36 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
-
Default/System Date Format Pattern?
By Gajesh Tripathi in forum AWT / SwingReplies: 1Last Post: 10-05-2008, 07:34 AM -
How to format the date in particular pattern
By Java Tip in forum java.textReplies: 0Last Post: 04-04-2008, 02:35 PM -
How to format the date in particular pattern
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks