Date types in Java
Util Date(java.util.Date)
The util date class represents a timestamp of a particular moment. it is a value in milliseconds which represents the number of milliseconds since the 1st of January 1970(Epoch Time). It lets you to interpret the year, month, day, hour second of a particular point of time. With the introduction of the DateTime class and the Calendar class most of the functionality of the util date class has been depreciated.
SQL Date(java.sql.Date)
Java SQL date is an Wrapper around the util date library and it inherits the java.util.date class. It is meant to be used only when dealing with databases. it is a millisecond value which represents the number of milliseconds since the 1st of January 1970 (Epoch Time). The SQL Date class interprets the year, month and day, but it does not keep the time data.
Calendar Class
The calendar class provides functionality to convert a given point of time into a year, month, the day of the month, hour etc. It represents a point of time similar to the Epoch time.
java.time Package
The Classes included in this package depicts the fundamental concepts of date and time such as dates, times, time zones, durations and instants. It is based on the ISO Calendar system and follows the Gregorian rules. All of the classes included are thread safe and immutable. Some of the classes included in this package are ZonedDateTime, LocalDateTime etc. Every class has functions to support parsing date and times and also support for printing. This package was introduced in version 1.8
LocalDateTime Class (java.time.LocalDateTime)
The LocalDateTime class represents the date and time with nanosecond precision. and allows to represent time as year, month, date, hour, etc. It does not contain a time zone and is used to describe a date of a particular event. It is a value based class in which comparisons can be done.
ZonedDateTime Class (java.time.ZonedDateTime)
The ZonedDateTime class stores all of the date and time fields which is precise up to nanoseconds. This class also stores the time zone of a particular date. It has fields such as ZoneId which stores the time zone (eg. Europe) and ZoneOffset (eg. +05:30). The ZonedTimeZone class provides the functionality to convert the time at a given time zone to a different time zone. The ZonedDateTime class also takes the daylight saving time into consideration, and have methods such as plusHours and minusHours
Epoch Time
Epoch time is a way of representing a time in which an event is occurred. Epoch time is a timestamp which represents the number of seconds from the 1st of January 1970. it does not have a time zone hence the epoch time is the same in any location of the world. Epoch time is used so that the time represented would not be varied by the time zones.
Get Shashane Ranasinghe’s stories in your inbox
Join Medium for free to get updates from this writer.
The development of the Epoch time occurred during the late 1960s and was initially released in 1971. At the time the early UNIX systems could only hold a limited number of days. This was due to the fact that time was measured in fractions of a second. due to this reason the start time for the epoch time had to be a recent date. Therefore they selected the 1st of January 1971 as the start date of the epoch time. but later on they were able to measure time in intervals of one second which made it possible to hold a larger range of days and hence the start time didn't have to be a recent date. So for convenience the starting date of the Epoch time was moved to the 1st of January 1970.
There are some computers that use an 32-bit System. even though a 32 bit can represent a large number by the year 2038 the epoch value will be larger than what a 32 bit number could represent. therefore the 32 nit systems would face some problems. The users will have to migrate to an 64-bit system so that they can avoid this problem for a couple a billion years. This problem is know as the year 2038 problem
Time of death problem
Example:
A person dies in Colombo, Sri Lanka on a Sunday at 6 AM. A group of relatives of the person who lives in San Francisco, USA received the notice of death which says the time of death was 6 AM Sunday. But it is still Saturday in San Francisco, which means the death has not occurred yet. This is known as the time of death problem
The reason for this issue is that they have not considered the time difference and the time zones. This problem can be avoided by using time zones. As per the example we can say that the person died at 6AM Sunday(GMT+5:30) so then when we inform the relatives living in the US this issue would not occur.
References
- https://www.codingem.com/epoch-time/
- https://moviecultists.com/why-use-epoch-time
- https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html
- https://www.javatpoint.com/java-sql-date
- https://www.baeldung.com/java-util-date-sql-date
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html









