Results 1 to 7 of 7
  1. #1
    javanewbie12 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Unhappy 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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
    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;
    }
    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.

    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.

  4. #4
    javanewbie12 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  5. #5
    javanewbie12 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    travishein,

    thank you. this will give me a great start. I appreciate it.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    javanewbie12 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 02:59 PM
  2. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 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
  •