![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between java.sql.Time, java.sql.Timestamp, and java.sql.Date in Java
In the world of Java programming, overseeing dates and times is a regular task. The java.sql group gives three classes - java.sql.Time, java.sql.Timestamp, and java.sql.Date - that are explicitly intended to manage various pieces of date and time control. Nonetheless, understanding the distinctions between these classes is urgent to guarantee precise treatment of worldly information in your Java applications. In this article, we will explore the nuances of each class and examine their remarkable functionalities.
Approach
While working with transient information, it is critical to pick the right class in light of the particular prerequisites of your application. Each class in the java.sql package serves a particular need and offers various elements. By understanding their differences, you can select the appropriate class and utilize its functionalities effectively.
Common Algorithm and Syntax
For general date and time works out, you can utilize the 'java.util.Date' class, which is the base class for 'java.sql.Time', 'java.sql.Timestamp', and 'java.sql.Date'.
Syntax (Creating a Date Object)
java.util.Date date = new java.util.Date();
Syntax (Getting Current Date and Time)
java.util.Date currentDate = new java.util.Date();
Algorithm
Make another example of the 'java.util.Date' class.
The subsequent article addresses the ongoing date and time.
You can perform different procedures on the 'java.util.Date' object, like formatting, comparison, and manipulation.
Types of Approach
java.sql.Time
The java.sql.Time class addresses a particular season of day in hours, minutes, and seconds.
It broadens the java.util.Date class and gives accuracy to the milliseconds.
It stores no data about the date part, just the time.
Syntax
java.sql.Time time = new java.sql.Time(hours, minutes, seconds);
Algorithm
Create a new instance of the java.sql.Time class.
Make another example of the java.sql.Time class.
Pass the hours, minutes, and seconds as boundaries to the constructor.
Example
import java.sql.Time; public class TimeExample { public static void main(String[] args) { // Creating a Time object Time time = new Time(System.currentTimeMillis()); System.out.println("Current Time: " + time); // Extracting individual components int hours = time.getHours(); int minutes = time.getMinutes(); int seconds = time.getSeconds(); System.out.println("Hours: " + hours); System.out.println("Minutes: " + minutes); System.out.println("Seconds: " + seconds); } }
Output
Note: TimeExample.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Current Time: 11:46:05 Hours: 11 Minutes: 46 Seconds: 5
java.sql.Timestamp
The java.sql.Timestamp class extends java.util.Date and provides nanosecond precision.
It addresses a particular moment, including both date and time parts.
It is equipped for saving up to the nanosecond level and can deal with a wide range of dates.
Syntax
java.sql.Timestamp timestamp = new java.sql.Timestamp(year, month, day, hours, minutes, seconds, nanoseconds);
Algorithm
Make another occasion of the java.sql.Timestamp class.
Pass the year, month, day, hours, minutes, seconds, and nanoseconds as boundaries to the constructor.
The subsequent item addresses a particular moment, including both date and time parts with nanosecond accuracy.
Example
import java.sql.Timestamp; public class TimestampExample { public static void main(String[] args) { // Creating a Timestamp object Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println("Current Timestamp: " + timestamp); // Extracting individual components int year = timestamp.getYear() + 1900; // Adding 1900 as getYear() returns the year since 1900 int month = timestamp.getMonth() + 1; // Adding 1 as getMonth() returns zero-based month int day = timestamp.getDate(); int hours = timestamp.getHours(); int minutes = timestamp.getMinutes(); int seconds = timestamp.getSeconds(); System.out.println("Year: " + year); System.out.println("Month: " + month); System.out.println("Day: " + day); System.out.println("Hours: " + hours); System.out.println("Minutes: " + minutes); System.out.println("Seconds: " + seconds); } }
Output
Note: TimestampExample.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Current Timestamp: 2023-07-31 11:47:08.731 Year: 2023 Month: 7 Day: 31 Hours: 11 Minutes: 47 Seconds: 8
java.sql.Date
The java.sql.Date class addresses a particular date (year, month, and day) without a period part.
It broadens java.util.Date and is usually used to store and control date values in data sets.
It upholds dates going from January 1, 1970, to December 31, 9999.
Syntax
java.sql.Date date = java.sql.Date.valueOf("yyyy-mm-dd");
Algorithm
Create a new instance of the java.sql.Date class.
Use the `valueOf()` method and pass a string in the format "yyyy-mm-dd" representing the desired date.
The resulting object represents a specific date without a time component.
Example
import java.sql.Date; public class DateExample { public static void main(String[] args) { // Creating a Date object Date date = new Date(System.currentTimeMillis()); System.out.println("Current Date: " + date); // Extracting individual components int year = date.getYear() + 1900; // Adding 1900 as getYear() returns the year since 1900 int month = date.getMonth() + 1; // Adding 1 as getMonth() returns zero-based month int day = date.getDate(); System.out.println("Year: " + year); System.out.println("Month: " + month); System.out.println("Day: " + day); } }
Output
Note: DateExample.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Current Date: 2023-07-31 Year: 2023 Month: 7 Day: 31
Difference Table
Aspect |
java.sql.Time |
java.sql.Timestamp |
java.sql.Date |
---|---|---|---|
Representation |
Specific time of day |
Specific point in time |
Specific date |
Precision |
Milliseconds |
Nanoseconds |
Day (up to milliseconds) |
Date Component |
No |
Yes |
Yes |
Time Component |
Yes |
Yes |
No |
Use Case |
Handling time-only values |
Handling both date and time |
Handling date-only values |
Format |
HH:mm:ss |
yyyy-MM-dd HH:mm:ss.SSS |
yyyy-MM-dd |
Constructor |
Time(int hour, int minute, int second) |
Timestamp(long milliseconds) |
Date(long milliseconds) |
Conversion to String |
Uses default time format |
Uses default timestamp format |
Uses default date format |
Compatibility |
Extends java.util.Date |
Extends java.util.Date |
Extends java.util.Date |
Conclusion
Java programming requires careful selection of appropriate classes to handle temporal data effectively and accurately. In the domain of java.sql, three unmistakable classes, in particular java.sql.Time, java.sql.Timestamp, and java.sql.Date, fill explicit needs. The java.sql.Time class relates to the exact season of day, java.sql.Timestamp addresses a specific second in time, while java.sql.Date compares to a particular date barring the time part. Understanding the subtleties that put these classes aside engages designers to pursue informed choices in view of their application's exceptional prerequisites.Armed with this knowledge, Java programmers gain the ability to manipulate and persist temporal data with precision and reliability, ensuring the seamless operation of their software systems.