jdk/src/share/classes/java/sql/Timestamp.java
changeset 15658 55b829ca2334
parent 11129 f9ad1aadf3fa
child 23010 6dadb192ad81
equal deleted inserted replaced
15657:c588664d547e 15658:55b829ca2334
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.sql;
    26 package java.sql;
    27 
    27 
       
    28 import java.time.Instant;
       
    29 import java.time.LocalDateTime;
    28 import java.util.StringTokenizer;
    30 import java.util.StringTokenizer;
    29 
    31 
    30 /**
    32 /**
    31  * <P>A thin wrapper around <code>java.util.Date</code> that allows
    33  * <P>A thin wrapper around <code>java.util.Date</code> that allows
    32  * the JDBC API to identify this as an SQL <code>TIMESTAMP</code> value.
    34  * the JDBC API to identify this as an SQL <code>TIMESTAMP</code> value.
   483             } else if (nanos < ts.nanos) {
   485             } else if (nanos < ts.nanos) {
   484                 return -1;
   486                 return -1;
   485             }
   487             }
   486         }
   488         }
   487         return i;
   489         return i;
   488 
       
   489     }
   490     }
   490 
   491 
   491     /**
   492     /**
   492      * Compares this <code>Timestamp</code> object to the given
   493      * Compares this <code>Timestamp</code> object to the given
   493      * <code>Date</code> object.
   494      * <code>Date</code> object.
   528         return super.hashCode();
   529         return super.hashCode();
   529     }
   530     }
   530 
   531 
   531     static final long serialVersionUID = 2745179027874758501L;
   532     static final long serialVersionUID = 2745179027874758501L;
   532 
   533 
       
   534     private static final int MILLIS_PER_SECOND = 1000;
       
   535 
       
   536     /**
       
   537      * Obtains an instance of {@code Timestamp} from a {@code LocalDateTime}
       
   538      * object, with the same year, month, day of month, hours, minutes,
       
   539      * seconds and nanos date-time value as the provided {@code LocalDateTime}.
       
   540      * <p>
       
   541      * The provided {@code LocalDateTime} is interpreted as the local
       
   542      * date-time in the local time zone.
       
   543      *
       
   544      * @param dateTime a {@code LocalDateTime} to convert
       
   545      * @return a {@code Timestamp} object
       
   546      * @exception NullPointerException if {@code dateTime} is null.
       
   547      * @since 1.8
       
   548      */
       
   549     @SuppressWarnings("deprecation")
       
   550     public static Timestamp valueOf(LocalDateTime dateTime) {
       
   551         return new Timestamp(dateTime.getYear() - 1900,
       
   552                              dateTime.getMonthValue() - 1,
       
   553                              dateTime.getDayOfMonth(),
       
   554                              dateTime.getHour(),
       
   555                              dateTime.getMinute(),
       
   556                              dateTime.getSecond(),
       
   557                              dateTime.getNano());
       
   558     }
       
   559 
       
   560     /**
       
   561      * Converts this {@code Timestamp} object to a {@code LocalDateTime}.
       
   562      * <p>
       
   563      * The conversion creates a {@code LocalDateTime} that represents the
       
   564      * same year, month, day of month, hours, minutes, seconds and nanos
       
   565      * date-time value as this {@code Timestamp} in the local time zone.
       
   566      *
       
   567      * @return a {@code LocalDateTime} object representing the same date-time value
       
   568      * @since 1.8
       
   569      */
       
   570     @SuppressWarnings("deprecation")
       
   571     public LocalDateTime toLocalDateTime() {
       
   572         return LocalDateTime.of(getYear() + 1900,
       
   573                                 getMonth() + 1,
       
   574                                 getDate(),
       
   575                                 getHours(),
       
   576                                 getMinutes(),
       
   577                                 getSeconds(),
       
   578                                 getNanos());
       
   579     }
       
   580 
       
   581     /**
       
   582      * Obtains an instance of {@code Timestamp} from an {@link Instant} object.
       
   583      * <p>
       
   584      * {@code Instant} can store points on the time-line further in the future
       
   585      * and further in the past than {@code Date}. In this scenario, this method
       
   586      * will throw an exception.
       
   587      *
       
   588      * @param instant  the instant to convert
       
   589      * @return an {@code Timestamp} representing the same point on the time-line as
       
   590      *  the provided instant
       
   591      * @exception NullPointerException if {@code instant} is null.
       
   592      * @exception IllegalArgumentException if the instant is too large to
       
   593      *  represent as a {@code Timesamp}
       
   594      * @since 1.8
       
   595      */
       
   596     public static Timestamp from(Instant instant) {
       
   597         try {
       
   598             Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
       
   599             stamp.nanos = instant.getNano();
       
   600             return stamp;
       
   601         } catch (ArithmeticException ex) {
       
   602             throw new IllegalArgumentException(ex);
       
   603         }
       
   604     }
       
   605 
       
   606     /**
       
   607      * Converts this {@code Timestamp} object to an {@code Instant}.
       
   608      * <p>
       
   609      * The conversion creates an {@code Instant} that represents the same
       
   610      * point on the time-line as this {@code Timestamp}.
       
   611      *
       
   612      * @return an instant representing the same point on the time-line
       
   613      * @since 1.8
       
   614      */
       
   615     @Override
       
   616     public Instant toInstant() {
       
   617         return Instant.ofEpochSecond(super.getTime() / MILLIS_PER_SECOND, nanos);
       
   618     }
   533 }
   619 }