Handling Date and Time in Java
Time is a more precious power in the world. We all are carried our life to follow by the time.
When dealing time with software it’s tricky hard to keep maintaining the current date and time. Because the time zone is different everywhere. Developers were hard to build the software for different time zone.
In the early stages, In java, the date and time data are stored in the form of a long data type number. Because of that, the number of seconds can be shown as positive and negative ( long represented both positive and negative signs).
There are some pre-build libraries (classes )are used to define the time zone in applications.
Those are:
- java.util.Date
- java.util.TimeZone
- java.util.Calendar
- java.text.SimpleDateFormat
These libraries are available in java.util, java.time, java.sql, java.text packages.
These libraries are used up to java 7. But when developers have used these legacy classes to build an application they faced some difficulties.
Those are :
1. Date formatting Issue
The Date class used to represent a date but it represents time( hours, minutes, and seconds )also.
2. Time zone handling issue
The date doesn’t associate with the time zone. It takes the default time zone. Hence we cannot represent a date for different time zones.
3. No Thread-safe
When we trying to use Date and time formatting in java, java.util classes are not thread-safe. Therefore it is difficult to handle for use.
4. Zero index-based approach Fails.
Date classes used to zero index for a month, this will take several bugs in sometimes.
Hence the reason, Java 8 provides Date and Time API to solve these drawbacks with legacy classes.
Java 8 Date Time API
How does java 8 handle Date?
Java 8 introduces 2 classes for handle those issues ;
- ZoneId implements the IDs of time zones. For example ZoneId.of(“Asia/India”).
- ZoneDateTime implements the human-readable representation of an instant in the ISO 8601 calendar, in a given time zone. For example: Instant.now().atZone(zoneId)
Java 8 provides Date Time API to developers for build applications. This Date Time API located in java.time package.This java.time package contains some sub-packages those are having multi utilities. For that java.time.format package contains some classes used to parse and format dates form to string.
The main changes in Java 8 is no long data type is used to represent by single number of milliseconds.
But the number of nanoseconds is always positive and its representation by using int data type. It used before and after java 8
In java.time package contains various class likes Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth, and so on.
- java.time.LocalTime- It’s an immutable class and represents the time with the default format of hour-minute-second. only represent the time based on ISO 8601 calendar.
- java.time.LocalDate- Year-month-date represents in the ISO calendar. Only the date will be shown (Without shown in time). This time was read in a human-readable manner.
- java.time.LocalDateTime- Date-time object that represents both date and time with the format yyyy-MM-dd-HH-mm-ss.zzz.
- java.time.ZonedDateTime- It’s represented with date and time-zone. It’s used to store all date-time fields to precision nanoseconds. (LocalDateTime combine with timezone to represent it)
- java.time.OffsetTime- Represents the time view format as hour-minute-second as time zone offset without showing time zone ID.
- java.time.OffsetDateTime- It represents the Date with the corresponding time zone without showing zone ID.
- java.time.Clock- Provides to access the current instant, date, and time using any given time zone.
- java.time.Instant- Represent the specific moment on the timeline.
- java.time.Duration- Used to measure the time in seconds and nanoseconds. Doesn’t use the date-based construct such as years, months, and day rather provides methods to convert in days, hours, and minutes.
- java.time.Period- Used to define the difference between dates in date-based value (years, months, days)
- java.time.ZoneId- specifies a time zone identifier and provides rules for converting between an Instant and a LocalDateTime.
- java.time.ZoneOffset- specifies a time zone offset from Greenwich/UTC time.
Date Time APIs Operations
LocalDateTime, LocalDate, LocalTime these objects are immutable objects, hence that we cannot apply those operators, But it is important to add, subtract, and calculations for date and time, Java has provided operations for the objects. For that, plusX() can use for addition, minusX() can use for subtraction. X that can be replaced “Years, Months, Hours, Days”. And “isBefore()” or “isAfter()” methods are used for comparing with dates.
Formatting & Parsing Date and Time
Using java.time package to get the date and time for the different time zone based on each geographical location. But this not enough to represent the time and date, so need a standard way to represent the time zone.
For that geographical location difference, people using a variety of standard languages. So that reason application should be adjustable to represent the local language and format. Java provides java.util.Locale class that supported in the Java environment to represent the languages and countries. So first it setting the Locale to format time into the local format, then java gave java.time.format.DateTimeFormatter, to present the date and time as a standard way or custom way.
Let’s take an example code to see formatting the Date according to different countries and languages.
The output will be shown in different date formats in different countries and languages.
Let’s see another example :
Above code, set the locale to some countries with different date formats and languages, Then use DateTimeFormatter to set the different date format based on the locate. Finally, format the given date and assign it to a String object for representation.
The output will like this:
References: