Results 1 to 7 of 7
Thread: Date application - help please!
- 12-08-2009, 01:40 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Date application - help please!
I am trying to complete the following but have no idea where to start. Help please. I am very new to java and only have a basic understanding. Thanks.
Write an application in which the user can enter a date using digits and slashes ("2/4/2010"), and receive output that displays the date with the month shown as a word (such as "February 4, 2010). Allow for the fact that the user might or might not precede a month or day with a zero (for example, the user might type "02/04/2010" or "2/4/2010"). Do not allow the user to enter an invalid date, defined as one for which the month is less than 1 or more than 12, or one for which the day number is less than 1 or greater than the number of days in the specified month. Also display the date's ordinal position in the year; for example, 2/4 is the 35th day.
Thank you for any assistance you can provide.
-
- 12-08-2009, 01:51 AM #3
You would want to read in a string, then send it into a SimpleDateFormat instance that is configured with some formatting codes to interpret the date in the input format (see the Java API documentation for this for all the formatting codes to use)
This will parse the string expression into a java.util.Date object. When in object form, we would use a second SimpleDateFormat instance, configured with a different formatting code, to format the date object back into a string of the required output formatting.
To handle situations where the user might input one of many formats, it might be good to set up an array of formats to check. When an invalid date is intered the dateformatter will throw a ParseException. We can use this as a kind of indication when a correct date format has been entered.
For example, the input format reader might be something like
In this example, the method will return null when it was not able to parse the input string into a date. your invoking stuff will need to inspect this and handle this case by informing the user they entered an invalid date and make them try again.Java Code:public date parseInputDate(String dateIn) { DateFormat[] inputFormats = new DateFormat[] { new SimpleDateFormat("M/d/yyyy"), new SimpledateFormat("MM/dd/yyyy"), // other formats if we need them here }; Date result = null; for (DateFormat fmt : inputFormats) { try { result = fmt.parse(dateIn); break; // if we get here, were able to parse the date correctly. } catch (ParseException ex) { // empty } } // for return result; }
Similarily, the output date formatter object would just use the format() method (after being set up with the correct formatting string, to render the proper output format.
- 12-08-2009, 01:52 AM #4
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Thanks. I actually know how to write a program. Just not this kind. It's way over my head. Arrays confuse me and there are no examples in the book I'm using that cover dates in this sort of a program.
- 12-08-2009, 01:53 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
travishein,
thank you. this will give me a great start. I appreciate it.
-
This sounds like a homework assignment, and I'm going to make a guess that SimpleDateFormat isn't allowed here, but again, it's just a guess. I'm also going to state that I'll bet you didn't read my link fully as it goes into more than just how to write a program.
- 12-08-2009, 02:07 AM #7
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
java.util.Date vs java.sql.Date
By Jack in forum New To JavaReplies: 5Last Post: 10-28-2010, 02:59 PM -
julian date to full date format
By judy318 in forum New To JavaReplies: 7Last Post: 11-02-2009, 12:17 PM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks