Example #1
0
 public void testParseDateFormat()
 {
     java.text.SimpleDateFormat sdf    = new java.text.SimpleDateFormat("dd.MM.yyyy");
     java.text.ParsePosition    zeroPP = new java.text.ParsePosition(0);
     java.util.Date             result = null;
     result = sdf.parse("05.09.1975", zeroPP);
     Assert.IsNotNull(result);
     Assert.AreEqual <int>(75, result.getYear(), "Year 75 expected");
     Assert.AreEqual <int>(9, result.getMonth(), "Month 9 expected");
     Assert.AreEqual <int>(5, result.getDate(), "Day of month 5 expected");
 }
 public void testParseDateFormat()
 {
     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd.MM.yyyy");
     java.text.ParsePosition zeroPP = new java.text.ParsePosition (0);
     java.util.Date result = null;
     result = sdf.parse("05.09.1975", zeroPP);
     Assert.IsNotNull(result);
     Assert.AreEqual<int>(75, result.getYear(), "Year 75 expected");
     Assert.AreEqual<int>(9, result.getMonth(), "Month 9 expected");
     Assert.AreEqual<int>(5, result.getDate(), "Day of month 5 expected");
 }
Example #3
0
        /**
         * Creates a {@code Timestamp} object with a time value equal to the time
         * specified by a supplied String holding the time in JDBC timestamp escape
         * format, which is {@code "yyyy-mm-dd hh:mm:ss.nnnnnnnnn}"
         *
         * @param s
         *            the {@code String} containing a time in JDBC timestamp escape
         *            format.
         * @return A {@code Timestamp} object with time value as defined by the
         *         supplied {@code String}.
         * @throws IllegalArgumentException
         *             if the provided string is {@code null}.
         */
        public static Timestamp valueOf(String s)
        {
            if (s == null)
            {
                // sql.3=Argument cannot be null
                throw new java.lang.IllegalArgumentException("Argument cannot be null"); //$NON-NLS-1$
            }

            // omit trailing whitespaces
            s = s.trim();
            if (!java.util.regex.Pattern.matches(TIME_FORMAT_REGEX, (java.lang.CharSequence) new java.lang.StringJ(s)))
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
            java.text.ParsePosition    pp = new java.text.ParsePosition(0);

            /*
             * First parse out the yyyy-MM-dd HH:mm:ss component of the String into
             * a Date object using the SimpleDateFormat. This should stop after the
             * seconds value, according to the definition of SimpleDateFormat.parse,
             * with the ParsePosition indicating the index of the "." which should
             * precede the nanoseconds value
             */
            java.util.Date theDate;
            try
            {
                theDate = df.parse(s, pp);
            }
            catch (Exception e)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            if (theDate == null)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            /*
             * If we get here, the Date part of the string was OK - now for the
             * nanoseconds value. Strictly, this requires the remaining part of the
             * String to look like ".nnnnnnnnn". However, we accept anything with a
             * '.' followed by 1 to 9 digits - we also accept nothing (no fractions
             * of a second). Anything else is interpreted as incorrect format which
             * will generate an IllegalArgumentException
             */
            int position  = pp.getIndex();
            int remaining = s.length() - position;
            int theNanos;

            if (remaining == 0)
            {
                // First, allow for the case where no fraction of a second is given:
                theNanos = 0;
            }
            else
            {
                /*
                 * Case where fraction of a second is specified: Require 1 character
                 * plus the "." in the remaining part of the string...
                 */
                if ((s.length() - position) < ".n".length())
                {                                                                                                           //$NON-NLS-1$
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }

                /*
                 * If we're strict, we should not allow any EXTRA characters after
                 * the 9 digits
                 */
                if ((s.length() - position) > ".nnnnnnnnn".length())
                {                                                                                                           //$NON-NLS-1$
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }

                // Require the next character to be a "."
                if (s.charAt(position) != '.')
                {
                    // sql.4=Bad input string format: expected '.' not {0}
                    throw new java.lang.NumberFormatException("Bad input string format: expected '.' not " + s.charAt(position)); //$NON-NLS-1$
                }
                // Get the length of the number string - need to account for the '.'
                int nanoLength = s.length() - position - 1;

                // Get the 9 characters following the "." as an integer
                String theNanoString = s.substring(position + 1, position + 1
                                                   + nanoLength);

                /*
                 * We must adjust for the cases where the nanos String was not 9
                 * characters long by padding out with zeros
                 */
                theNanoString = theNanoString + "000000000"; //$NON-NLS-1$
                theNanoString = theNanoString.substring(0, 9);

                try
                {
                    theNanos = java.lang.Integer.parseInt(theNanoString);
                }
                catch (Exception e)
                {
                    // If we get here, the string was not a number
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }
            }

            if (theNanos < 0 || theNanos > 999999999)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            Timestamp theTimestamp = new Timestamp(theDate.getTime());

            theTimestamp.setNanos(theNanos);

            return(theTimestamp);
        }
Example #4
0
        /**
         * Creates a {@code Timestamp} object with a time value equal to the time
         * specified by a supplied String holding the time in JDBC timestamp escape
         * format, which is {@code "yyyy-mm-dd hh:mm:ss.nnnnnnnnn}"
         *
         * @param s
         *            the {@code String} containing a time in JDBC timestamp escape
         *            format.
         * @return A {@code Timestamp} object with time value as defined by the
         *         supplied {@code String}.
         * @throws IllegalArgumentException
         *             if the provided string is {@code null}.
         */
        public static Timestamp valueOf(String s)
        {
            if (s == null)
            {
                // sql.3=Argument cannot be null
                throw new java.lang.IllegalArgumentException("Argument cannot be null"); //$NON-NLS-1$
            }

            // omit trailing whitespaces
            s = s.trim();
            if (!java.util.regex.Pattern.matches(TIME_FORMAT_REGEX, (java.lang.CharSequence)new java.lang.StringJ(s)))
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
            java.text.ParsePosition pp = new java.text.ParsePosition(0);

            /*
             * First parse out the yyyy-MM-dd HH:mm:ss component of the String into
             * a Date object using the SimpleDateFormat. This should stop after the
             * seconds value, according to the definition of SimpleDateFormat.parse,
             * with the ParsePosition indicating the index of the "." which should
             * precede the nanoseconds value
             */
            java.util.Date theDate;
            try
            {
                theDate = df.parse(s, pp);
            }
            catch (Exception e)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            if (theDate == null)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            /*
             * If we get here, the Date part of the string was OK - now for the
             * nanoseconds value. Strictly, this requires the remaining part of the
             * String to look like ".nnnnnnnnn". However, we accept anything with a
             * '.' followed by 1 to 9 digits - we also accept nothing (no fractions
             * of a second). Anything else is interpreted as incorrect format which
             * will generate an IllegalArgumentException
             */
            int position = pp.getIndex();
            int remaining = s.length() - position;
            int theNanos;

            if (remaining == 0)
            {
                // First, allow for the case where no fraction of a second is given:
                theNanos = 0;
            }
            else
            {
                /*
                 * Case where fraction of a second is specified: Require 1 character
                 * plus the "." in the remaining part of the string...
                 */
                if ((s.length() - position) < ".n".length())
                { //$NON-NLS-1$
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }

                /*
                 * If we're strict, we should not allow any EXTRA characters after
                 * the 9 digits
                 */
                if ((s.length() - position) > ".nnnnnnnnn".length())
                { //$NON-NLS-1$
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }

                // Require the next character to be a "."
                if (s.charAt(position) != '.')
                {
                    // sql.4=Bad input string format: expected '.' not {0}
                    throw new java.lang.NumberFormatException("Bad input string format: expected '.' not " + s.charAt(position)); //$NON-NLS-1$
                }
                // Get the length of the number string - need to account for the '.'
                int nanoLength = s.length() - position - 1;

                // Get the 9 characters following the "." as an integer
                String theNanoString = s.substring(position + 1, position + 1
                        + nanoLength);
                /*
                 * We must adjust for the cases where the nanos String was not 9
                 * characters long by padding out with zeros
                 */
                theNanoString = theNanoString + "000000000"; //$NON-NLS-1$
                theNanoString = theNanoString.substring(0, 9);

                try
                {
                    theNanos = java.lang.Integer.parseInt(theNanoString);
                }
                catch (Exception e)
                {
                    // If we get here, the string was not a number
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }
            }

            if (theNanos < 0 || theNanos > 999999999)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            Timestamp theTimestamp = new Timestamp(theDate.getTime());
            theTimestamp.setNanos(theNanos);

            return theTimestamp;
        }