How to write a java8 program to implement Date and Time API?

     Each and every nanosecond is important in computer operations. So Date and Time calculation plays a vital role in computer programming. Java8 offers a new Date and Time API for the enhancement of built in packages java.util.Date and java.util.Calendar.

 What are the new features added in these API???

  • ·       Concise methods: It consists of simple and concise methods.
  • ·       Wide range of classes:  This API has variety of classes for different date and time zones.
  • ·       Thread safe API :If the instance is created, it is not changeable.

List of important classes:

              API includes wide range of built in classes. Here, the important classes are listed down.

Built in Class

Purpose

Format

Eg

LocalTime

It provides a time without date.

Hours:Minutes:Seconds

21:32:14

LocalDate

It gives a date without time

Year – Month - Day

2024-09-11

LocalDateTime

It shows Date and Time without any time zone

Year- Month- Day

THours:Minutes:Seconds

2024-09-11

21:32:14T

ZonedDateTime

It displays Date and Time with time zone

Year- Month- Day

THours:Minutes:Seconds +05:30[ Asia/Kolkata]

2024-09-11

21:32:14

+05:30[ Asia/Kolkata]

 

How to write a java8 program to implement Date and Time API?

Date and time API implementation can be done by two ways.

  1.    Displaying Date and Time

  2.       Implementing ZoneddateTime

Each implementation is given below.

 1.    Displaying Date and Time:

This java8 Program displays Date and Time. It includes the following steps.

·       Include the built in packages java.time.LocalDate,java.time.LocalTime, java.time.LocalDateTime.

·       Create a public class “DateTimeEg” and save this file as “DateTimeEg.java”.

·       Inside the main function, create three objects for LocalDate,LocalTime, LocalDateTime as date1,time1,dateTime1.

·       Using the objects,call the now function for each objects by as follows.

·       date1.now(),time1.now(), dateTime.now().

·       Print the date,time and  datetime.

Here, is the program.

Java8 program to display date,time,datetime :

import java.time.LocalDate;

import java.time.LocalTime;

import java.time.LocalDateTime;

public class DateTimeEg {

    public static void main(String[] args) {

        LocalDate date1 = LocalDate.now();

        LocalTime time1 = LocalTime.now();

        LocalDateTime dateTime1 = LocalDateTime.now();

        System.out.println("Today's Date is: " + date1);

        System.out.println("The Current Time is: " + time1);

        System.out.println("The date and time is: " + dateTime1);

    }

}

Open the command prompt. Set the path. Compile and Execute the program as follows.

C:\raji\blog>javac DateTimeEg.java

C:\raji\blog>java DateTimeEg

Today's Date is: 2024-09-11

The Current Time is: 09:41:16.593843

The date and time is: 2024-09-11T09:41:16.593843

2.Implementing ZoneddateTime

              This java8 program deals with ZoneDateand time. To implement zonedDateTime,follow the steps.

  • Import the built in packages “java.time.ZonedDateTime, java.time.ZoneId”.
  • Create a class “ZonedDateTimeEg” and save this file as “ZonedDateTimeEg.java”.
  • Inside the main function, create object for ZonedDateTime and call the now function.
  • Display the current zone time with print statement.

#Java Program to implement ZoneddateTime

import java.time.ZonedDateTime;

import java.time.ZoneId;

public class ZonedDateTimeEg {

    public static void main(String[] args) {

        ZonedDateTime zDateTime1 = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));

        System.out.println("The Current Date and Time in Asia/Kolkata zone is: " + zDateTime1);

    }

}

Follow the steps to get the output.

C:\raji\blog>javac ZonedDateTimeEg.java

C:\raji\blog>java ZonedDateTimeEg

The Current Date and Time in Asia/Kolkata zone is: 2024-09-11T10:23:18.010432100+05:30[Asia/Kolkata]

These are all the ways to implement date and time API in java.

No comments:

Post a Comment