In java, both the java.util
and the java.sql
package contain a Date
class, So what is the difference between them?
If one Date
class is present in Java then what is the need of another Date
class?
sql. Date just represent DATE without time information while java. util. Date represents both Date and Time information.
The java. util. Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time). The class is used to keep coordinated universal time (UTC).
Find the time difference between two dates in millisecondes by using the method getTime() in Java as d2. getTime() – d1. getTime(). Use date-time mathematical formula to find the difference between two dates.
Java provides the Date class available in java. util package, this class encapsulates the current date and time.
From the JavaDoc of java.sql.Date
:
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.
To conform with the definition of SQL DATE, the millisecond values wrapped by a
java.sql.Date
instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
Explanation: A java.util.Date
represents date and time of day, a java.sql.Date
only represents a date (the complement of java.sql.Date is java.sql.Time
, which only represents a time of day, but also extends java.util.Date
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With