Date and Time in Java
Before talking about the date and time in java, let's discuss a bit about why this matters in java and what are the possible problems that could be created when not adhering to the correct date and time standard.
Think about the following scenario.
A Doctor in Sri Lanka updates a record of a patient and the time is recorded as 10.00 am, April 30( Actual time in SL at the time of the entry). Another doctor who is in the USA may see this recorded time (Just after updating by the previous doctor) and get confused. That is because the current actual time in USA is 1.00 am, April 30.
So by the above scenario, it's clear that we cannot use date and time as it is. The required changes need to considered.
How the time is calculated for different countries based on their location?
Coordinated Universal Time (UTC) is the primary time standard by which the world regulates clocks and time. This is used as a standard. So as an example, the IST time (Indian Standard Time) is calculated by adding 5.30 hours to the UTC time.
So now it's clear to us that we cannot store date and time as it is.
In Java, there are several packages that contain classes for representing date and time.
- java.time
- java.util
- java.sql
- java.text
Shortcomings of old Date and calendar API in Java :
- Use of strings to represent Time Zones. They should be a constant.
- The calendar class is not very user-friendly. Cannot apply common sense or predictive knowledge.
- Difficulty in formatting dates.
- The Date and calendar classes are not thread-safe.
NEW JAVA 8 DATE/TIME API
The new Java Date/Time API addresses these shortcomings present in the old Date and calendar API.
There are various classes present. LocalDate, LocalTime, and LocalDateTime are some of the most commonly used classes.
LocalDate
Represents the date in ISO format without time. (ISO Format: yyyy-mm-dd). Used to store dates and provides various utility methods to obtain various information.
- .now() — Get current date and store it.
- .now().plusDays(x) — Add “x” number of days to the current date and store it.
- .getDayOfWeek() — Get the day of the week.
- .isLeapYear() — Check for a leap year.
There are many methods like this.
LocalTime
Represents time without a date. Similar to LocalDate, LocalTime also supports many different utility methods.
- .getHour()
- .isBefore()
- .MAX
LocalDateTime
Represents a combination of date and time. This class offers various APIs for different functionalities. LocalDateTime instance can be obtained from the system clock.
ZonedDateTime API
Representation of a date-time with a time zone. Immutable in nature and used to store all the date and time fields to a precision of nanoseconds.
- Zoneid — Identifier to represent different Time zones.
Period class
Quantity of time in terms of years, months, and days. Widely used get the difference between two particular dates or to modify a date.
getYears(), getDays(), getMonths() can be used to get values from an object created using Period class.
Duration class
Quantity of time in terms of seconds and nanoseconds.
Joda-Time Library
This is an alternative to the Date and Time library. Provides almost all the functionalities available in the Java DateTime library.
Daylight Saving Time and How it affects Java Applications
Daylight Saving Time is the practice of advancing clocks in order to make the darkness fall at a later time. This fact should be taken into consideration when working with Date and time in Java applications. This process happens often and the change must be recorded in the IANA Time zone Database. Then this change will be updated in the JRE from the next release.
Java Time Zone Updater Tool — A tool that can be used to modify Time zone data inside JRE without waiting for a new release.
However, the above-mentioned LocalDateTime API is completely unaware of the Daylight Saving Time. The programmers need to do the necessary conversions.