Hi guys, Let’s discuss the concept of Date and Time in Java 8. Well, it was already there then why again in java 8? That is because previously we have many issues in working. So, there are many improvements in java 8 which we will see here.
Advantages over old Date-Time API
First, we see how the new API is better than the older one:
Since old date-time was not thread-safe. So we have to deal with concurrency issues while using date and to write additional code to handle thread-safety. So in Java 8 date and time are immutable and thread-safe, thus taking that concurrency headache away from us.
It had no uniformity. As default Date starts from 1900, the month starts from 1, and the day starts from 0. It had inadequate methods to perform day-to-day operations and there were fewer operations present. The new date-time API is ISO centric and follows consistent domain models for time, dates, duration, and periods and there is a wide variety of utility methods for such operations.
We had to write a lot of code to handle the zoning issue. But in new API handling of timezone can be done with Local and ZonedDate APIs.
Let’s perform some basic hands-on:
We can have handle Date and Time in 3 ways:
– LocalDate: LocalDate represents a date in ISO format (yyyy-mm-dd) without time.
– LocalTime: LocalTime represents time without a date.
– LocalDateTime: LocalDateTime is used to represent a combination of date and time.
//current date LocalDate today = LocalDate.now(); System.out.println("Today's date is "+ date); //current time LocalTime currentTime = LocalTime.now(); System.out.println("Current time is "+ time); //current time and date LocalDateTime currentDate = LocalDateTime.now(); System.out.println("date and time : "+ current); //a particular format DateTimeFormatter newFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formatedDate = currentDate.format(newFormat); System.out.println("Foramatted Date "+ formatedDate); //some specified date LocalDate birthday = LocalDate.of(1998,5,26); System.out.println("My Birthday is :"+birthday); Output: Today's date is 2020-06-17 Current time is 06:21:10.409 date and time : 2020-06-17T06:21:10.410 Foramatted Date 17-06-2020 06:21:10 Month : APRIL day : 9 seconds : 10 My Birthday is :1998-05-26
This is really useful when we need to deal with timezone specific dates and times.
Following zone ids are present:
// to get the current zone ZonedDateTime currentZone = ZonedDateTime.now(); System.out.println("the current zone is "+ currentZone.getZone()); // getting time zone of specific place ZoneId kolkata = ZoneId.of("Asia/Kolkata"); ZonedDateTime kolkataZone = currentZone.withZoneSameInstant(kolkata); System.out.println("kolkata time zone is " + kolkataZone); //formatting time zone of specific place DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formatedDateTime = kolkataZone.format(format); System.out.println("formatted kolkata time zone "+ formatedDateTime); Output: the current zone is Asia/Kolkata kolkata time zone is 2020-06-17T19:01:03.259099+05:30[Asia/Kolkata] formatted kolkata time zone 17-06-2020 19:01:03
Period and Duration
Period: Period is most suitable when we have to deal with Dates.
Duration: Duration is most suitable when we have to deal with Time.
LocalDate today = LocalDate.now(); LocalDate date = LocalDate.of(2020, Month.DECEMBER, 12); Period period = Period.between(date, today); System.out.println("period between dates is a period of "+period); LocalTime now = LocalTime.now(); System.out.println("the current time is " + now); Duration twoHours = Duration.ofHours(2); // adding two hours to the current-time and storing it in later LocalTime later = now.plus(twoHours); System.out.println("after adding two hours of duration " + later); Duration duration = Duration.between(later, now); System.out.println("duration between now & later is " + duration); Output: period between dates is a period of P-5M-25D the current time is 19:16:44.643179 after adding two hours of duration 21:16:44.643179 duration between now & later is PT-2H
Chrono Units Enum
Chrono Units Enum is added in Java 8 to replace integer values used in old API to represent days, month, etc.
//add 1 hour to the current time LocalTime nextHour = now.plus(1,ChronoUnit.HOURS); System.out.println("Next hour: " + nextHour); //add 1 week to the current date LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS); System.out.println("Next week: " + nextWeek); //add 1 month to the current date LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS); System.out.println("Next month: " + nextMonth); //add 10 year to the current date LocalDate nextYear = today.plus(10, ChronoUnit.YEARS); System.out.println("Tenth year: " + nextYear); Output: Next hour: 20:34:38.675949 Next week: 2020-06-24 Next month: 2020-07-17 Tenth year: 2030-06-17