Results 1 to 4 of 4
- 02-03-2013, 11:05 PM #1
Member
- Join Date
- Jan 2013
- Location
- Texas
- Posts
- 41
- Rep Power
- 0
How to make sure a date string is in a certain order.... way too many possibilities
Greetings all,
So I have a main method and it asks the user to input a date and then sends the information to the MyDateClass to do some processing. In this exercise, we cannot use java date classes, we have to create our own. And mine is working but I am trying to make it so that when the user inputs the date, I can sort it out better. I mean they could input the date as dd/mm/yy or dd/mm/yyyy or mm/dd/yyyy or yy/mm/dd since it is a string. I am stumped on how to look at this so I can check to ensure they are putting a date format that I can work with. One of the ways I am going to have them input the way is with Integers, but we have to allow them to also input a date via a String.
I am just looking for guidance on the best approach to handling this type of situation. I have set it up to split the string on a / or a - or a " " but what if in their minds they entered this: 10/2/2013 that could be either October 2 or February 10th. If that makes sense, I would appreciate any advice. I already know I can complete the project but I want to do more than just a minimum job.
This points to MyDateClassJava Code:import java.util.Scanner; public class SteadmanProject2_draft { /** * @param args the command line arguments */ public static void main(String[] args) { String userDate; System.out.println("Input a Date:\n"); Scanner input = new Scanner(System.in); userDate = input.nextLine(); MyDateClass firstDate = new MyDateClass(userDate); System.out.println(firstDate.getMonth()); System.out.println(firstDate.getDay()); System.out.println(firstDate.getYear()); } }
Java Code:public class MyDateClass { private int month; private int day; private int year; private static int counter = 0; public MyDateClass(int month, int day, int year) { this.month = month; this.day = day; this.year = year; } public MyDateClass(String date) { String dateSplit[] = date.split("[/\\- ]"); month = Integer.valueOf(dateSplit[0]); day = Integer.valueOf(dateSplit[1]); year = Integer.valueOf(dateSplit[2]); } public MyDateClass() { month = 1; day = 1; year = 1970; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; counter++; } public int getDay() { return day; } public void setDay(int day) { this.day = day; counter++; } public int getYear() { return year; } public void setYear(int year) { this.year = year; counter++; } public static int getCounter() { return counter; } }
- 02-04-2013, 12:44 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: How to make sure a date string is in a certain order.... way too many possibiliti
The one thing you can rely upon is the stupidity of users!
Instead of attempting to cope with every possible data entry, you can tell the user what is acceptable. Provide the user with specific instrutions on how you want the date formatted and validate this before splitting it (i.e. "Please enter a date in the format..."). If its not valid (i.e. they entered 01 02 56 instead of a requested 01/02/56) then reject the input and ask for a new one.
If the user enters 10/2/2013 to mean 2nd October instead of 10th February then thats something you can't handle. The only thing you could do to resolve this is ask for confirmation of the input after validation to ensure this is the input the user intended.
"There are no stupid questions, just stupid people". When all else fails, blame it on a layer 8 issue ;) (the user).
Regards.
- 02-04-2013, 05:19 AM #3
Member
- Join Date
- Jan 2013
- Location
- Texas
- Posts
- 41
- Rep Power
- 0
Re: How to make sure a date string is in a certain order.... way too many possibiliti
Thanks for the feedback. I was planning on doing what you suggested already but your post firms it up. I love the Layer 8 reference. Never heard that one before.... I use to say the issue was PEBKAC - Problem exists between keyboard and computer. Going to use layer 8 going forward. Thanks.
- 02-04-2013, 07:25 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: How to make sure a date string is in a certain order.... way too many possibiliti
By allowing a two digit year you're in big trouble; e.g. 04/02/13: is that the 4th of February 2013? 13th of February 2004? April 2nd 2013? You can't tell by just analyzing the String; it's ambiguous ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
FTPClient listFiles - how to get list order by name or date
By s3c in forum Apache CommonsReplies: 2Last Post: 12-05-2012, 07:21 PM -
string date to formatted date string
By reach2sudhakar in forum New To JavaReplies: 4Last Post: 06-20-2011, 10:06 AM -
converting string (GMT date) to date in US time.
By JRuyechan in forum New To JavaReplies: 1Last Post: 10-15-2010, 07:07 AM -
convert String date to Date
By computerbum in forum New To JavaReplies: 7Last Post: 09-18-2010, 03:26 PM -
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks