×

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL TIME value. Time is set to 1970 on January 1st as its starting value. Time after that has all positive values, whereas time before has negative ones.

time-class-operation-in-java

Constructors:

By Time(long t) constructs a Time object by utilizing a milliseconds time valueava Program

Timeclass1.java

// Java program to explain the constructor of Time Class
import java.sql.* ;
class Timeclass1{
	public static void main (String s [ ])
	{
		// milliseconds 
		long m = 123456789999l;
		// Object creation for the class
		java.sql.Time t1 = new java.sql.Time(m) ;
		// printing the time
		System.out.println ( " Time = " + t1.toString( ) ) ;
	}
}

Output

time-class-operation-in-java

Methods

MethodExplanation
setTime(long t)  The millisecond value of time is set using the function  .
toString()  JDBC time escape format is applied to a time using the action toString().  
valueOf()  A string in JDBC time escape format is converted to a Time value via valueOf(String s).  
setTime()  Sets a Time object with a milliseconds time value using the settime ng time) function.
Syntax
public void setTime(long time) 
The obligatory parameter time, which indicates the time to be set in milliseconds since January 1, 1970, 00:00:00 GMT, is accepted by this method.  
valueOf()  This method, valueOf(String s), turns a string in JDBC time escape format into a Time value. Syntax: public static Value of time (String s) The obligatory parameter s, which represents the time in the format "hh:mm:ss," is accepted by this method.
Return Value:
An appropriate Time object is returned by this method.  
toString ()  JDBC time escape format is used in the function toString()  method to format a time.
Syntax
public String to String ()
Return Value:
The hh:mm:ss formatted String is returned by this procedure.  

Java program

import java.sql.*;


class Timeclass2 {
	public static void main(String args[])
	{


		long m = 123413789999l;


		// create an object
		java. sql. Time t = new java. sql. Time ( m ) ;


		// display the time
		System . out. println ( " Time = " + t . toString ( ) ) ;


		long ms = 476415454l;
		t . setTime ( ms ) ;


		System . out. println ( " New Time = " + t . toString ( ) ) ;


		System . out. println ( " Value of 00:04:29 = " + Time.valueOf ("00:04:29" )  ) ;
	}
}

Before the release of Java 8, the main classes were:

Java. Lang.

System: The current time in milliseconds is returned by the class's currentTimeMillis() method. From January 1st, 1970, it displays the current date and time in milliseconds.

java.util.Date

Date: It is used to display a precise moment in milliseconds.

Java 8 adds a new Date Time API.

The new date API overcomes the shortcomings of the old classes listed above.

java.time.LocalDate

LocalDate: It is useful for displaying dates without times because it represents a year, month, and day on the ISO calendar. It can be used to describe information with a single date, such as birth date or wedding date.

java.time.LocalTime

LocalTime: It simply deals with time. It helps display the time of day based on humans, such as movie times or the hours that the nearby library is open and closed.

java.time.LocalDateTime

LocalDateTime: It manages date and time without considering a time zone. It is the result of combining LocalDate and LocalTime.

java.time.ZonedDateTime

It combines the LocalDateTime class with the zone information found in ZoneId to create ZonedDateTime.

java.util.Calendar

Calendar: This abstract class offers techniques to change the values of the calendar fields and convert them between instances.

Java.text.SimpleDateFormat

SimpleDateFormat: This class formats and parses dates according to the user- or predefined-pattern definitions.

java.util.TimeZone

TimeZone: It calculates daylight savings time and represents a time zone offset.

java.time.OffsetDateTime

Without a time zone ID, OffsetDateTime manages dates and times with an appropriate time zone offset from Greenwich/UTC.

java.time.Clock

Clock: It gives users access to the time, date, and instant in any selected time zone. This capability enables us to test your code for additional time zones or by utilizing a fixed clock, where time does not change, even though the Clock class is optional.


Related Topics

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.