Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/sql/Time.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627import java.time.Instant;28import java.time.LocalTime;2930/**31* <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC32* API to identify this as an SQL <code>TIME</code> value. The <code>Time</code>33* class adds formatting and34* parsing operations to support the JDBC escape syntax for time35* values.36* <p>The date components should be set to the "zero epoch"37* value of January 1, 1970 and should not be accessed.38*/39public class Time extends java.util.Date {4041/**42* Constructs a <code>Time</code> object initialized with the43* given values for the hour, minute, and second.44* The driver sets the date components to January 1, 1970.45* Any method that attempts to access the date components of a46* <code>Time</code> object will throw a47* <code>java.lang.IllegalArgumentException</code>.48* <P>49* The result is undefined if a given argument is out of bounds.50*51* @param hour 0 to 2352* @param minute 0 to 5953* @param second 0 to 5954*55* @deprecated Use the constructor that takes a milliseconds value56* in place of this constructor57*/58@Deprecated59public Time(int hour, int minute, int second) {60super(70, 0, 1, hour, minute, second);61}6263/**64* Constructs a <code>Time</code> object using a milliseconds time value.65*66* @param time milliseconds since January 1, 1970, 00:00:00 GMT;67* a negative number is milliseconds before68* January 1, 1970, 00:00:00 GMT69*/70public Time(long time) {71super(time);72}7374/**75* Sets a <code>Time</code> object using a milliseconds time value.76*77* @param time milliseconds since January 1, 1970, 00:00:00 GMT;78* a negative number is milliseconds before79* January 1, 1970, 00:00:00 GMT80*/81public void setTime(long time) {82super.setTime(time);83}8485/**86* Converts a string in JDBC time escape format to a <code>Time</code> value.87*88* @param s time in format "hh:mm:ss"89* @return a corresponding <code>Time</code> object90*/91public static Time valueOf(String s) {92int hour;93int minute;94int second;95int firstColon;96int secondColon;9798if (s == null) throw new java.lang.IllegalArgumentException();99100firstColon = s.indexOf(':');101secondColon = s.indexOf(':', firstColon+1);102if ((firstColon > 0) & (secondColon > 0) &103(secondColon < s.length()-1)) {104hour = Integer.parseInt(s.substring(0, firstColon));105minute =106Integer.parseInt(s.substring(firstColon+1, secondColon));107second = Integer.parseInt(s.substring(secondColon+1));108} else {109throw new java.lang.IllegalArgumentException();110}111112return new Time(hour, minute, second);113}114115/**116* Formats a time in JDBC time escape format.117*118* @return a <code>String</code> in hh:mm:ss format119*/120@SuppressWarnings("deprecation")121public String toString () {122int hour = super.getHours();123int minute = super.getMinutes();124int second = super.getSeconds();125String hourString;126String minuteString;127String secondString;128129if (hour < 10) {130hourString = "0" + hour;131} else {132hourString = Integer.toString(hour);133}134if (minute < 10) {135minuteString = "0" + minute;136} else {137minuteString = Integer.toString(minute);138}139if (second < 10) {140secondString = "0" + second;141} else {142secondString = Integer.toString(second);143}144return (hourString + ":" + minuteString + ":" + secondString);145}146147// Override all the date operations inherited from java.util.Date;148149/**150* This method is deprecated and should not be used because SQL <code>TIME</code>151* values do not have a year component.152*153* @deprecated154* @exception java.lang.IllegalArgumentException if this155* method is invoked156* @see #setYear157*/158@Deprecated159public int getYear() {160throw new java.lang.IllegalArgumentException();161}162163/**164* This method is deprecated and should not be used because SQL <code>TIME</code>165* values do not have a month component.166*167* @deprecated168* @exception java.lang.IllegalArgumentException if this169* method is invoked170* @see #setMonth171*/172@Deprecated173public int getMonth() {174throw new java.lang.IllegalArgumentException();175}176177/**178* This method is deprecated and should not be used because SQL <code>TIME</code>179* values do not have a day component.180*181* @deprecated182* @exception java.lang.IllegalArgumentException if this183* method is invoked184*/185@Deprecated186public int getDay() {187throw new java.lang.IllegalArgumentException();188}189190/**191* This method is deprecated and should not be used because SQL <code>TIME</code>192* values do not have a date component.193*194* @deprecated195* @exception java.lang.IllegalArgumentException if this196* method is invoked197* @see #setDate198*/199@Deprecated200public int getDate() {201throw new java.lang.IllegalArgumentException();202}203204/**205* This method is deprecated and should not be used because SQL <code>TIME</code>206* values do not have a year component.207*208* @deprecated209* @exception java.lang.IllegalArgumentException if this210* method is invoked211* @see #getYear212*/213@Deprecated214public void setYear(int i) {215throw new java.lang.IllegalArgumentException();216}217218/**219* This method is deprecated and should not be used because SQL <code>TIME</code>220* values do not have a month component.221*222* @deprecated223* @exception java.lang.IllegalArgumentException if this224* method is invoked225* @see #getMonth226*/227@Deprecated228public void setMonth(int i) {229throw new java.lang.IllegalArgumentException();230}231232/**233* This method is deprecated and should not be used because SQL <code>TIME</code>234* values do not have a date component.235*236* @deprecated237* @exception java.lang.IllegalArgumentException if this238* method is invoked239* @see #getDate240*/241@Deprecated242public void setDate(int i) {243throw new java.lang.IllegalArgumentException();244}245246/**247* Private serial version unique ID to ensure serialization248* compatibility.249*/250static final long serialVersionUID = 8397324403548013681L;251252/**253* Obtains an instance of {@code Time} from a {@link LocalTime} object254* with the same hour, minute and second time value as the given255* {@code LocalTime}.256*257* @param time a {@code LocalTime} to convert258* @return a {@code Time} object259* @exception NullPointerException if {@code time} is null260* @since 1.8261*/262@SuppressWarnings("deprecation")263public static Time valueOf(LocalTime time) {264return new Time(time.getHour(), time.getMinute(), time.getSecond());265}266267/**268* Converts this {@code Time} object to a {@code LocalTime}.269* <p>270* The conversion creates a {@code LocalTime} that represents the same271* hour, minute, and second time value as this {@code Time}.272*273* @return a {@code LocalTime} object representing the same time value274* @since 1.8275*/276@SuppressWarnings("deprecation")277public LocalTime toLocalTime() {278return LocalTime.of(getHours(), getMinutes(), getSeconds());279}280281/**282* This method always throws an UnsupportedOperationException and should283* not be used because SQL {@code Time} values do not have a date284* component.285*286* @exception java.lang.UnsupportedOperationException if this method is invoked287*/288@Override289public Instant toInstant() {290throw new java.lang.UnsupportedOperationException();291}292}293294295