Results 1 to 4 of 4
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

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

    Java 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());
      }
    }
    This points to MyDateClass

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

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

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

  3. #3
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. string date to formatted date string
    By reach2sudhakar in forum New To Java
    Replies: 4
    Last Post: 06-20-2011, 10:06 AM
  2. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  3. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 03:26 PM
  4. Replies: 7
    Last Post: 11-03-2009, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •