Results 1 to 9 of 9
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default SimpleDateFormat()

    all,

    I have this code to validate a date in short format ("MM/dd/yyyy"):

    Java Code:
    String pattern;
    pattern = "MM/dd/yyyy");
    
    inDate = JOptionPane.showInputDialog("enter");
    
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.setLenient(false);
    
    try
    {
      sdf.applyPatter(pattern);
      Date date = sdf.parse(inDate); //check for an exception
    }
    catch
    {
      System.out.println("Wrong!");
      System.exit(0);
    }
    These dates ARE caught:

    12/12/0000
    32/12/2013
    12/55/2013
    3847463528
    jdiehgltoehg

    However, this is NOT:

    12/12/38475647

    My question: is the parser for this object really this bad?? I am not using a typical environment. I am using an environment called BlueJ. It's extremely simple and not the best, but I haven't had a problem with until now, if this really *is* a real problem with Java in general.

    thanks guys.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: SimpleDateFormat()

    Hi ajetrumpet,

    The reason for this is that the code does not validate the length of the year, instead your input is being seen as a literal. I'll let the extract below explain how a date is interpreted by the pattern:

    Year: If the formatter's Calendar is the Gregorian calendar, the following rules are applied.

    •For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number.

    •For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

    •For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by Character.isDigit(char), will be parsed into the default century. Any other numeric string, such as a one digit string, a three or more digit string, or a two digit string that isn't all digits (for example, "-1"), is interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
    Otherwise, calendar system specific forms are applied. For both formatting and parsing, if the number of pattern letters is 4 or more, a calendar specific long form is used. Otherwise, a calendar specific short or abbreviated form is used
    Full details can be found here:
    SimpleDateFormat (Java Platform SE 7 )

    Regards.
    Last edited by Ronin; 03-29-2013 at 04:54 PM. Reason: Formatting

  3. #3
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: SimpleDateFormat()

    thanks Roonin.

    That will suffice. Once again the explanation text is much longer than need be. grrrrrrr

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: SimpleDateFormat()

    Quote Originally Posted by ajetrumpet View Post
    thanks Roonin.

    That will suffice. Once again the explanation text is much longer than need be. grrrrrrr
    Criticising members for providing a full explanation is hardly going to win you any friends!
    This is an open forum where the information could be useful to other people besides just yourself.

    Regards.

  5. #5
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: SimpleDateFormat()

    Quote Originally Posted by Ronin View Post
    Criticising members for providing a full explanation is hardly going to win you any friends!
    This is an open forum where the information could be useful to other people besides just yourself.

    Regards.
    Ronin don't worry about it. That was not the point. I was simply saying that the java docs on Oracle's website are once again *way* too long in order to get the information we need for this small simple little issue. That's the frustration. I very much appreciate your response. It was all the help I needed.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: SimpleDateFormat()

    Then I misunderstood the direction of the rant for which I apologies.
    Agreed, the Java docs are a bit much especially for beginners and they do get some getting used to.

    Regards.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: SimpleDateFormat()

    Quote Originally Posted by ajetrumpet View Post
    I was simply saying that the java docs on Oracle's website are once again *way* too long in order to get the information we need for this small simple little issue. That's the frustration.
    You're not going far in programming if you don't have the patience to read and understand documentation.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: SimpleDateFormat()

    Quote Originally Posted by DarrylBurke View Post
    You're not going far in programming if you don't have the patience to read and understand documentation.
    If documentation is short an browsable but lacks detailed explanation when I need it, that documentation belongs in a fire place. Especially reference documentation should contain all that a person should want to know about the class/interface/enum. Besides, a couple of pages max isn't a whole lot of reading ...

    kind regards,

    Jos (<-- iron eyeballs)
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: SimpleDateFormat()

    Considering the relevant part was in the second and third sentences complaining that the description is too long is, frankly, rather odd.
    I don't think it could be any shorter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. SimpleDateFormat Problem
    By UnfairBear in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-12-2011, 11:50 AM
  2. Problem with SimpleDateFormat
    By johan313 in forum Advanced Java
    Replies: 3
    Last Post: 01-17-2011, 06:20 PM
  3. JTextField and SimpleDateFormat
    By mine0926 in forum New To Java
    Replies: 15
    Last Post: 07-06-2010, 04:10 AM
  4. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 11:53 AM
  5. Using SimpleDateFormat
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-18-2008, 09:33 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
  •