Time class is a part of Java SQL package.This class is a thin wrapper around java.util.Date that allows JDBC API to identify this as a SQL TIME value. The initial value of time is set to 1st January, 1970. All values of time after that is positive and time before that is negative.
Class Hierarchy:
java.lang.Object
↳ java.util.Date
↳ java.sql.Time
Constructors:
- Time(long t): creates a Time object using a milliseconds time value.
Example to demonstrate how to create object of Time Class using Constructor:
// Java program to demonstrate // Constructor of Time Class import java.sql.*; class GFG { public static void main(String args[]) { // time in milliseconds long milli = 123456789999l; // create a object java.sql.Time time = new java.sql.Time(milli); // display the time System.out.println("Time = " + time.toString()); } } |
Time = 21:33:09
Methods:
| Method | Explanation |
|---|---|
| setTime(long t) | sets the value of time using milliseconds |
| toString() | Formats a time in JDBC time escape format. |
| valueOf(String s) | Converts a string in JDBC time escape format to a Time value. |
- setTime(long time): This method sets a Time object using a milliseconds time value.
Syntax:
public void setTime(long time)
Parameters: This method accepts a mandatory parameter time which represents the time to be set in milliseconds since January 1, 1970, 00:00:00 GMT.
- valueOf(String s): This method converts a string in JDBC time escape format to a Time value.
Syntax:
public static Time valueOf(String s)
Parameters: This method accepts a mandatory parameter s which represents the time in format “hh:mm:ss”
Return Value: This method returns a corresponding Time object
- toString(): This method formats a time in JDBC time escape format.
Syntax:
public String toString()
Return Value: This method returns a String in hh:mm:ss format.
- Java AWT | CardLayout Class
- Java AWT | Desktop Class
- Java AWT | BorderLayout Class
- Java AWT | Choice Class
- Java AWT | Cursor class with examples
- Implementing a Linked List in Java using Class
- JavaFX | Reflection Class
- JavaFX | Dimension2D Class
- JavaFX | Point3D Class
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java 8 | DoubleToIntFunction Interface in Java with Example
Program to demonstrate setTime(), valueOf() and toString() methods:
// Java program to demonstrate // setTime(), valueOf() and toString() methods: import java.sql.*; class GFG { public static void main(String args[]) { // time in milliseconds long milli = 123456789999l; // create a object java.sql.Time time = new java.sql.Time(milli); // display the time System.out.println("Time = " + time.toString()); // ----- setTime() ----- // set another time // using setTime() method long milSec = 455415454l; time.setTime(milSec); // ----- toString() ----- // display the time // using toString() method System.out.println("New Time = " + time.toString()); // ----- valueOf() ----- // using valueOf() method System.out.println("Value of 00:05:29 = " + Time.valueOf("00:05:29")); } } |
Time = 21:33:09 New Time = 06:30:15 Value of 00:05:29 = 00:05:29
Reference: https://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



