MM/dd/yy SimpleDateFormat accepts 55/99/00

Hi,
I found out that MM/dd/yy would accept 55/22/80 instead of just allowing MM to be in the range of 01 to 12 same is the case with dd. If i specify aa/bb/cc it goes to the catch block. If I can make it to go to the catch block if the range of month and date is inappropriate I guess my code would work.
Here is a small java code I wrote for debugging.
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class checkDates {
     public static void main(String[] args) {
          String aMask = "mm/dd/yy";
SimpleDateFormat mydf = new SimpleDateFormat(aMask);
Date date;
String newEffDate = "25/99/80";
try {
               date = mydf.parse(newEffDate);
          System.out.println("Date is working" + newEffDate);
               } catch (ParseException pe) {
               System.out.println("Date is not working");
}

it is working if I enter date 03/55/05 where day 55 is incorrect and it goes to the catch block
but if i enter 55/31/05 where date is in proper range but MM (month) is not in proper range it is not going to the catch block

Similar Messages

  • SimpleDateFormat.parse accepts invalid date string

    SimpleDateFormat("dd/MM/yyyy").parse(...) accepts invalid input strings (e.g. "1.1.2gsd001") without raising an exception.
    Where I do a mistake?
    Example:
    import java.text.*;
    import java.util.*;
    public class Test {
    public static void main(String[] args) throws ParseException {
    String s = "1.1.2gsd001";
    SimpleDateFormat sdf = new SimpleDateFormat("d.M.yyyy");
    sdf.setLenient(false);
    Date d = sdf.parse(s);
    System.out.println(d);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(d);
    The result is: Sun Jan 01 00:00:00 CET 2
    Thanks for your help.

    Tahnks s lot.
    Final code is (and it workes well):
    import java.text.*;
    import java.util.*;
    public class Test {
      public static void main(String[] args) throws ParseException {
        ParsePosition pp = new ParsePosition(0);
        String s = "1.1.2001";
        SimpleDateFormat sdf = new SimpleDateFormat("d.M.yyyy");
        sdf.setLenient(false);
        Date d = sdf.parse(s, pp);
        if (pp.getIndex() != s.length())
          throw new ParseException(String.format("Unparseable date: %s", s), pp.getIndex());
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(d);
    }

  • Need info on SimpleDateFormat, Converting String to Date

    I'm a newbie and doing a conversion of a string to a date and it's adding a little less than 11 minutes. I know I'm missing something really simple but as I read it I ought to be able to convert "20030125 084539.637696" to Sat Jan 25 08:45:39 not 8:56! Also, for the time being I'm ignoring the zulu 'Z' because I can't find a pattern that'll take it.
    System.out.println("INFO:MetadataExtractorBean::filestarttime:" + filestarttime);
    filestarttime = filestarttime.replace("Z","");
    System.out.println("after filestarttime.replace(Z,null) filestarttime:" + filestarttime);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd hhmmss.SSSSSS");
    Date convertedDate = dateFormat.parse(filestarttime);
    System.out.println("after dateFormat.parse(filestarttime) convertedDate:" + convertedDate);
    INFO:MetadataExtractorBean::filestarttime:20030125 084539.637696Z
    after filestarttime.replace(Z,null) filestarttime:20030125 084539.637696
    after dateFormat.parse(filestarttime) convertedDate:Sat Jan 25 08:56:16 EST 2003
    Can someone help me with a) why it doesn't remain 8:45, and b) how to modify the pattern to be able to parse a zulu date. Thanks in advance.

    import java.text.*;
    public class ParsingExample {
        public static void main(String[] args) throws ParseException {
            String s = "20030125 084539.637";
            SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
            System.out.println(f.parse(s));
    }Your round up was because ".637696" was taken to mean 637,696 milliseconds. SimpleDateFormat doesn't have a way to accept microseconds, and anyway, java.util.Date only has millisecond precision. I also changed "hh" to "HH" because I assume this is with a 24 hour clock.
    edit: you can tell your date format not to do these roll ups by setting:
    f.setLenient(false);

  • TextField that accepts dates in DD-MM-YYYY format

    I need to create a texfield that accepts dates in DD-MM-YYYY. Also, if the input does not correspond to a date, I want to trap focus in the control....
    Thanks,
    V

    Oops, sorry for the previous post, it is not the code i am using...
    Here it is:
    import com.id.swing.mask.JMaskField;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.text.ParseException;
    import javax.swing.JComponent;
    import java.awt.Toolkit;
    import javax.swing.JOptionPane;
    import java.text.SimpleDateFormat;
    import java.sql.Date;
    import java.util.Locale;
    import java.awt.event.FocusListener;
    import java.awt.event.FocusEvent;
    public class IdDateMask extends JMaskField implements FocusListener {
    protected SimpleDateFormat m_sdfDbPattern;
    protected SimpleDateFormat m_sdfUserPattern;
    protected String m_userDatePattern = "ddMMyy";
    protected String m_mask = "{[0-1]}#/{[0-3]}#/##";
    protected boolean m_canBeNull;
    public IdDateMask(String mask, String userPattern) {
    super(mask);
    super.setLocale(new Locale(System.getProperty("id.pyrites.user.language"),
    System.getProperty("id.pyrites.user.country")));
    m_userDatePattern = userPattern;//ie ddMMyy
    m_canBeNull = true;
    initialize();
    protected void initialize(){
    setForeground(new Color(51, 0, 255));
    setDisabledTextColor(new Color(0, 0, 0));
    setColumns(8);
    setPreferredSize(new Dimension(84,20));
    setMinimumSize(new Dimension(27,20));
    setMaximumSize(new Dimension(128,20));
    m_sdfDbPattern = new SimpleDateFormat("yyyy-MM-dd", getLocale());
    m_sdfUserPattern = new SimpleDateFormat(m_userDatePattern, getLocale());
    m_sdfUserPattern.setLenient(false);
    public void focusGained(FocusEvent e) {
    this.setBackground(new Color(255,204,204));
    public void focusLost(FocusEvent e) {
    isValidDate();
    this.setBackground(Color.WHITE);
    public boolean canBeNull() {
    return m_canBeNull;
    public void setCanBeNull(boolean value) {
    m_canBeNull = value;
    public boolean isValidDate() {
    boolean retVal = false;
    try {
    String theStr = super.getText();//ie: 310802
    if(!canBeNull()) {
    if (theStr.equals("")) throw(new Exception());
    else m_sdfUserPattern.parse(theStr);//ParseException can be thrown here
    } else {
    if(!theStr.equals("")) System.out.println(m_sdfUserPattern.parse(theStr));//ParseException can be thrown here
    return true;
    } catch (ParseException ex) {
    System.out.println("IdDateMask - verifyDate() - ERROR: " + ex.getMessage());
    Toolkit.getDefaultToolkit().beep();
    try {
    JOptionPane.showMessageDialog(IdDateMask.this,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notValid") + m_userDatePattern,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notValidDlgTitle"),
    JOptionPane.ERROR_MESSAGE);
    } catch(Exception e) {
    System.out.println("IdDateMask - verifyDate() - I18N - ERROR: " + e.getMessage());
    JOptionPane.showMessageDialog(IdDateMask.this,
    "MSG_DATE_notValid" + m_userDatePattern,
    "MSG_DATE_notValidDlgTitle",
    JOptionPane.ERROR_MESSAGE);
    // super.setText("");
    // this.requestFocus();
    return false;
    } catch (Exception ex) {
    System.out.println("IdDateMask - verifyDate() - ERROR: " + ex.getMessage());
    Toolkit.getDefaultToolkit().beep();
    try {
    JOptionPane.showMessageDialog(IdDateMask.this,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notNull"),
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notNullDlgTitle"),
    JOptionPane.ERROR_MESSAGE);
    } catch(Exception e) {
    System.out.println("IdDateMask - verifyDate() - I18N - ERROR: " + e.getMessage());
    JOptionPane.showMessageDialog(IdDateMask.this,
    "MSG_DATE_notNull",
    "MSG_DATE_notNullDlgTitle",
    JOptionPane.ERROR_MESSAGE);
    // super.setText("");
    // this.requestFocus();
    return false;
    public void setToday() {
    setValue((new Date(System.currentTimeMillis())).toString());
    public void setText(String value) {
    setValue(value);
    public void setValue(String value) {
    if(!(value == null) && (!value.equals(""))) {
    //We receive the yyyy-MM-dd format...
    //The format of the displayed date is m_userDatePattern
    //So, we have to transform YYYY-MM-dd into for example ddMMYY
    //Let's do it.
    try {
    //If the field is empty, a ParseException is thrown
    String valueToDisplay = m_sdfUserPattern.format(m_sdfDbPattern.parse(value));
    super.setText(valueToDisplay);
    //Next we have to localize it according to medium format
    } catch (ParseException ex) {
    System.out.println("IdDateMask - setValue("+ value +") - ERROR: " + ex.getMessage());
    Toolkit.getDefaultToolkit().beep();
    try {
    JOptionPane.showMessageDialog(IdDateMask.this,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notValidInput") + value,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notValidInputDlgTitle"),
    JOptionPane.ERROR_MESSAGE);
    } catch(Exception e) {
    System.out.println("IdDateMask - setValue("+ value +") - I18N - ERROR: " + e.getMessage());
    JOptionPane.showMessageDialog(IdDateMask.this,
    "MSG_DATE_notValidInput" + value,
    "MSG_DATE_notValidInputDlgTitle",
    JOptionPane.ERROR_MESSAGE);
    } else {
    super.setText("");
    public String getText() {
    return getValue();
    public String getValue() {
    //Return date with format yyyy-MM-dd
    //But i have a String of format m_userDatePattern
    try {
    if(canBeNull() && (super.getText().equals(""))) return "";
    else {
    String outputDateStr = m_sdfDbPattern.format(m_sdfUserPattern.parse(super.getText()));
    return outputDateStr;
    } catch (ParseException ex) {
    System.out.println("IdDateMask - getValue() - ERROR: " + ex.getMessage());
    Toolkit.getDefaultToolkit().beep();
    try {
    JOptionPane.showMessageDialog(IdDateMask.this,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notValid") + m_userDatePattern,
    java.util.ResourceBundle.getBundle("com/id/pyrites/client/resources/Bundle", getLocale()).getString("MSG_DATE_notValidDlgTitle"),
    JOptionPane.ERROR_MESSAGE);
    } catch(Exception e) {
    System.out.println("IdDateMask - getValue() - I18N - ERROR: " + e.getMessage());
    JOptionPane.showMessageDialog(IdDateMask.this,
    "MSG_DATE_notValid" + m_userDatePattern,
    "MSG_DATE_notValidDlgTitle",
    JOptionPane.ERROR_MESSAGE);
    return "";
    public void setEnabled(boolean value) {
    super.setEnabled(value);
    //color section
    public void setBackgroundText(Color c) {
    setBackground(c);
    public void setForegroundText(Color c) {
    setForeground(c);

  • SimpleDateFormat 'S'

    My question regards using SimpleDateFormat to parse a Date object from a millisecond representation.
    The issue at hand is that I have a module that parses objects out of an XML representation where one of the tag attributes is a timestamp. The parser module can be configured such that the format of this timestamp is specified. No problem so far - however it is possible that one of the sources of the xml text gives the timestamp attribute in millisecond format: i.e. 93748404743.
    In SimpleDateFormat the 'S' symbol is for milliseconds, but not for the 'long' millisecond representation - rather for the milliseconds within the current second. That is, the range of acceptable values for the S portion of a formatted date is 0-999.
    Does anyone know of a way to have SimpleDateFormat to pickup this 'long' formatted date string?
    dlgrasse

    the previous two posts seem to point at doing a extra 'if' check. something like:
    Date timestamp = null;
    try
      long timestampLong = Long.parseLong (tstampStr);
      timestamp = new Date (timestampLong); // don't fuss if this is deprecated please :}
    catch (NumberFormatException nfe)
      timestamp = MY_SIMPLE_DATE_FORMAT.parse (timestampStr);
    }hmmm...if i don't get any better response i'll consider this route. however, from a programming point of view i hesitate because this uses 'exception handling as logic control'...something i personaly disdain and accept as bad practice.
    dlgrasse

  • Java.text.SimpleDateFormat millisecond problem...

    When I run this code:
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat();
    formatter.applyLocalizedPattern("yyyy-MM-dd HH:mm:ss.000000");
    java.util.Date date1 = formatter.parse("2001-08-14 13:49:38.000000", new java.text.ParsePosition(0) );
    System.out.println("Date 1 = " + date1);
    formatter.applyLocalizedPattern("yyyy-MM-dd HH:mm:ss.SSS000");
    java.util.Date date2 = formatter.parse("2001-08-14 13:49:38.000000", new java.text.ParsePosition(0) );
    System.out.println("Date 2 = " + date2);
    The output is:
    Date 1 = Tue Aug 14 13:49:38 GMT+01:00 2001
    Date 2 = null
    The only difference is in the localized pattern, date 1 is generated where milliseconds aren't in the localized pattern, date 2 is generated where milliseconds are in the localized pattern.
    Why is date2 null????

    Hi! When you use the second pattern, it appears that "yyyy-MM-dd HH:mm:ss.SSS000" is not an acceptable pattern. However, "yyyy-MM-dd HH:mm:ss.SSS" works. I'd hazard a guess that when you specify milliseconds, only three places are allowed.
    If you used pattern #2 as you had specified originally, a java.text.ParseException is thrown. With the above modification, it works as expected.
    Hope this helps!
    Cheers!

  • SimpleDateFormat can parse 2009102x

    Hello,
    If you look to the piece of code below, it seems my users can enter 2009102w.
    However, when one tries to enter 20091w20, it throws an error.
    I don't understand why an incorrect character in my date can get my date parsed?!
    Is this a bug or is there a reason?
              try {
                      DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                      //this is ok
                      //Date date = (Date)formatter.parse("20091020");
                      //this is also ok !!!!!
                      //Date date = (Date)formatter.parse("2009102w");
                      //this is not ok
                      //Date date = (Date)formatter.parse("20091w20");
                      System.out.println(date);
                  } catch (ParseException e) {
                       System.out.println("exception");
                  }Thanks

    The date formatter doesn't feel the need to parse all the characters you give it. If it reads an acceptable date it will ignore any garbage following. The Formatter objects in java.text seem to have been designed to parse a string with multiple elements, dates, numbers, etc., each taking it's own chunk. You'll find the same with the NumberFormat objects.
    Also, by default, the parsing mode on the data formatter is set to "lenient". If you want to tighten it up call setLenient(false) on the formatter. This is mostly about things like if you put 31/02/2009 then with lenient on you'll get 03/03/2009, but may affect the acceptance of a single digit day.
    P.S. if you really want to be strict about validating the format then check with a regexp first.
    Edited by: malcolmmc on Sep 8, 2009 8:59 AM

  • SimpleDateFormat parse error

    Hy,
    I've an error when parsing a date with the parse function of the SimpleDateFormat class.
    Date birthDate = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmZ");
    try {
         birthDate = sdf.parse("198012160810Z");
    } catch (ParseException e) {
         System.out.println(e.getMessage());
    The exception Unparseable date: "198012160810Z" is raised.
    I've no error if I remove the "Z" at the end of the date. I've checked in the RFC 822, the Z time zone is accepted.
    Does somebody knows the problem ?

    thegillou wrote:
    Thanks for your response everybody. I'ts OK.
    warnerja : I can't respond to say thank you when I sleep.....you know the timezones..........Sure, but I also know you were answered within 6 minutes after you posted the question. So you really are of the type to post a question and immediately go nighty-night rather than see if there will be replies soon? Hard to believe.
    Anyway, you're welcome.

  • SimpleDateFormat: Parse String To Date

    Hi
    I'm trying to parse Date-String to Date. But the Date-String I want to parse is embedded in a Text like:
    Bla bla bla bla bla bla 2004-05-05 bla bla bla bla bla.
    So I tried the attached Class to solve the problem, but it didn't work.
    Any ideas?
    Thanks Bona
    public class Test {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat();
    private String text;
    private Date date;
    public Test() {
    /ry {
    text = "Bla bla bla bla bla bla 2004-05-05 bla bla bla bla bla.";
    date = toDate(text, "yyyy-MM-dd");
    System.out.println("Datum: "+date);
    } catch (ParseException pe) {
    System.out.println(pe.getMessage());
    public static void main(String[] args) {
    Test test1 = new Test();
    public static Date toDate(String dateString, String dateFormatPattern)
    throws ParseException {
    Date date = null;
    dateFormat.applyPattern(dateFormatPattern);
    dateFormat.setLenient(true);
    date = dateFormat.parse(dateString);
    return date;
    }

    Well, you need to extract the date pattern from that String.
    You can use regular expressions to do that. See theRegex package for info on how to do that.
    BTW - AFAIK, lenient date parsing means, that the parser accepts stuff that matches the pattern, but contains illegal values like "2004-02-30" ...

  • Since upgrading my phone to the iOS7, I can no longer download music or apps via my phone or computer - on my phone, it wont let me accept the 'terms

    Since upgrading my phone to the iOS7, I can no longer download music or apps on neither my iphone or through itunes on my computer.  On my phone, it won't let me accept the terms & conditions.  On my computer, an error message keeps flashing up: 'Error (-50)'.  Please help?...

    Sounds like the device is in recovery mode. You will need to restore the phone. See this support document for instructions. http://support.apple.com/kb/HT1808

  • Trying to drag pdf files i have and combine them into one pdf file in the account i just purchased with Adobe. when i drag a pdf file over Adobe doesn't accept it. says it can not convert this type of file. but it is an Adobe file. Do I need to change it?

    Trying to drag pdf files i have and combine them into one pdf file in the account i just purchased with Adobe. when i drag a pdf file over Adobe doesn't accept it. says it can not convert this type of file. but it is an Adobe file. Do I need to change it in some other form befor dragging it?

    Hello djensen1x,
    Could you please let me know what version of Acrobat are you using.
    Also, tell me your workflow of combining those PDF files?
    Please share the screenshot of the error message that you get.
    Hope to get your response.
    Regards,
    Anubha

  • How can I access my iCloud email account when I don't remember the password and Apple won't accept the answer to my security question?

    I set up an email account with " @me.com". I get a wrong password message when I try to log in. When I try to change my password I get an INcorrect answer message to the security question I'm asked my birth date so I know I have it right, unless I did a typo when I set up the account. When I choose email me the reset password lnk I cannot retrieve the email because it won't accept the password. I wanted to justemail Apple for instructions but cannot find an email address for that. Any help would be appreciated.

    I tried that without any luck. I was hoping I could get Apple to reset it for me or delete the account so I could recreate it or at least tell me what is listed as my birth date, the security question answer.

  • My Outlook/iCloud calendar invites to others appear to work on my end and sync with my PC and mobile, but when other people "accept" the invite, it will not populate/add in to their calendar. How can i fix this without turning off iCloud?

    My Outlook/iCloud calendar invites to others appear to work on my end and sync with my PC and mobile, but when other people "accept" the invite, it will not populate/add in to their calendar. How can I fix this without turning off iCloud?
    I am at a new office that uses Outlook (not Outlook Exchange) which does not sync with my mobile... I just got iCloud set up on my PC to sync my contacts, calendar, reminders, etc... The sync worked (not without flaws, but the other issues seem solvable... I think), so that i can now see all my appointments on both my phone and on my PC. The problem I am having is that iCloud moved all of my calendar items from Outlook into iCloud calendar and now when I send out meeting/calendar invites the recipients may accept them, but the meeting does not get added to their calendar. This is a huge problem and may mean that i need to turn off iCloud.
    Does anyone know how to fix this?
    Thanks!

    I am replying to my own post here as I seem to have fixed the problem.
    I do have some calendars that are shared. Some of those are shared with users who have time zone support turned on. So i activated time zone support on my iphone, then deleted my icloud subscription. I then signed in to icloud again and voila... problem solved.
    It is a weird one as the other calendar views were always fine and when you opened an event that appeared in the wrong day (on list view), the correct date of the event was shown in the information...
    one more bug in a complicated system I guess

  • In iCal - I keep getting the error message " This calendar was created by Mail" and it will not accept any input; why..??

    In iCal - I keep getting the error message “ This calendar was created by Mail” and it will not accept any input; why..??

    use Disk Utility and Verify Permisions then fix and Verify Disk and fix, this should fix it.

  • How do I see calendar event responses (accept, decline,...)?

    Does Yosemite Calendar track the responses?  I just can't seem to find a way in the Calendar app itself on my macbook how to see who accepted, declined, maybe, or didn't respond at all yet. I see this very easily from my iPhone calendar app, just not on Mac. 
    Thanks!

    I'm not seeing where it shows me that.  Is it only the icons that tell me this?  There's no hover text or right click menu option that gives any other indication.

Maybe you are looking for