Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Good style question: accepting dates from the user Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Good style question: accepting dates from the user" Watch "Good style question: accepting dates from the user" New topic
Author

Good style question: accepting dates from the user

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

We all know that you never trust any data entered by the user. You have to parse it, and wrap it so you don't have the "little Bobby DropTables" problem.

Granted, I don't think the answer is critical for either performance or usability, but this question has popped into my brain.

Suppose the user is entering a date. The program doesn't know ahead of time what format it will be in, its up to the user.
2011-11-30 would be a nice format, but they are likely to entry Nov 30, 2011 or 11/30/2011 or durn near anything else.

I see two basic approaches. I'm wondering which is better, in the sense of style, good code, ease of understanding, or gross performance.

1) use a series of regex to identify which format its in, and then use that format in a SimpleDateFormat to pull out the values.
2) have a list of possible formats, and try them, in order, until you get a good value.

Other ideas?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Pat Farrell wrote: so you don't have the "little Bobby DropTables" problem.

Literal LOL!

I see two basic approaches.

I've used both approaches and from a performance standpoint never had an issue with either.

My current preferred approach is to use the jQuery UI datepicker where I can determine what the incoming format will be and users don't have to worry about formats in the first place.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

I agree with Bear regarding using a Date picker, I think giving the user concrete choices or options and also make it easier to use and less confusing for the end user. That way we might be able to get correct inputs from the user.


Mohamed Sanaulla | My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

HTML5 has a datetime form input type. Google Chrome has some support for it, Opera has excellent support for it (showing complete pickers for days, weeks and months). Unfortunately, that's as far as you'll get; Firefox still doesn't support it. I'd go with the jQuery picker as well. You still need to validate on the server, but at least you'll have one format to check.

Using multiple DateFormats works (using the parse method that takes a ParsePositoon, to prevent all the ParseExceptions), but you can't recognize all formats. Even worse would be language specific input; an English user could type "Nov 30 2011" or "Nov 30, 2011", but a Dutch user would use "30 nov 2011".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Rob Spoor wrote:.Even worse would be language specific input; an English user could type "Nov 30 2011" or "Nov 30, 2011", but a Dutch user would use "30 nov 2011".


That one is actually quite easy, as alphas and digits vary. Its not just language, the US military uses the sensible 30 nov 2011 format as well.

The hard ones are locale/tradition dependent but invisible to the computer. 03/06/2011, is that Mar 6 or Jun 3? Here you have to guess based on the locale of the user.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Basically the problem with guessing the user's date format -- which is what you're discussing -- is that you might guess wrong. So, let's do the risk analysis. What bad things could happen if you interpret a date as 2012 June 3 instead of 2012 March 6?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Paul Clapham wrote: What bad things could happen if you interpret a date as 2012 June 3 instead of 2012 March 6?


The worst is that the clueless user who used a dumb format gets an answer he didn't want. GIGO.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Pat Farrell wrote:
Paul Clapham wrote: What bad things could happen if you interpret a date as 2012 June 3 instead of 2012 March 6?


The worst is that the clueless user who used a dumb format gets an answer he didn't want. GIGO.


That sounds a lot less risky than having twelve truckloads of steel I-beams show up at a construction site three months before they were needed.
 
Consider Paul's rocket mass heater.
 
subject: Good style question: accepting dates from the user
 
Similar Threads
Running application on web start
Converting string to specific date format
JTable cell to show time only and change based on time entered (only time, disregard date)
java date format stored as varchar - need to convert it into date
Java Calendar Dates