Пример #1
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);
        }
Пример #2
0
 /**
  * Tests to see if this timestamp is equal to a supplied timestamp.
  *
  * @param theTimestamp
  *            the timestamp to compare with this {@code Timestamp} object,
  *            passed as an {@code Object}.
  * @return {@code true} if this {@code Timestamp} object is equal to the
  *         supplied {@code Timestamp} object, {@code false} otherwise.
  */
 public bool equals(Timestamp theTimestamp)
 {
     if (theTimestamp == null)
     {
         return false;
     }
     return (this.getTime() == theTimestamp.getTime())
             && (this.getNanos() == theTimestamp.getNanos());
 }
Пример #3
0
        /**
         * Returns {@code true} if this {@code Timestamp} object is earlier than the
         * supplied timestamp, otherwise returns {@code false}.
         *
         * @param theTimestamp
         *            the timestamp to compare with this {@code Timestamp} object.
         * @return {@code true} if this {@code Timestamp} object is earlier than the
         *         supplied timestamp, {@code false} otherwise.
         */
        public bool before(Timestamp theTimestamp)
        {
            long thisTime = this.getTime();
            long compareTime = theTimestamp.getTime();

            // If the time value is later, the timestamp is later
            if (thisTime < compareTime)
            {
                return true;
            }
            // If the time value is earlier, the timestamp is not later
            else if (thisTime > compareTime)
            {
                return false;
            }
            /*
             * Otherwise the time values are equal in which case the nanoseconds
             * value determines whether this timestamp is later...
             */
            else if (this.getNanos() < theTimestamp.getNanos())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Пример #4
0
 /**
  * Compares this {@code Timestamp} object with a supplied {@code Timestamp}
  * object.
  *
  * @param theTimestamp
  *            the timestamp to compare with this {@code Timestamp} object,
  *            passed in as a {@code Timestamp}.
  * @return one of the following:
  *         <ul>
  *         <li>{@code 0}, if the two {@code Timestamp} objects are
  *         equal in time</li>
  *         <li>{@code &lt; 0}, if this {@code Timestamp} object is before the
  *         supplied {@code Timestamp}</li>
  *         <li> {@code &gt; 0}, if this {@code Timestamp} object is after the
  *         supplied {@code Timestamp}</li>
  *         </ul>
  */
 public int compareTo(Timestamp theTimestamp)
 {
     int result = base.compareTo(theTimestamp);
     if (result == 0)
     {
         int thisNano = this.getNanos();
         int thatNano = theTimestamp.getNanos();
         if (thisNano > thatNano)
         {
             return 1;
         }
         else if (thisNano == thatNano)
         {
             return 0;
         }
         else
         {
             return -1;
         }
     }
     return result;
 }
Пример #5
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;
        }