Open In App

How to Add Time to Date in Java?

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, You can add the time to a Date object and it can be part of the java.util.Calendar package and another approach be used to newer java.time package. I can explain both approaches adding the time to a Date in Java programming. In this article, we are going to learn, how to add time to date in Java.

Methods to Add Time to Date in Java

There are two methods to add time to date in Java:

  • Using calendar
  • Using java.time

Approach 1: Method to add Time to Date in Java Using Calendar

Step-by-Step implementation:

  1. Create the instance of the calendar using Calendar.getInstance() method and add import packages.
  2. Set the time using a new date then add one hour.
  3. After that get the modified time using getTime() method.
  4. Print the results of the modified time and original time.

Below is the implementation of add Time to Date in Java Using Calendar

Java




// Java Program to add Time to Date in Java Using
// Calendar
import java.util.Calendar;
import java.util.Date;
  
public class ExampleCalender {
    public static void main(String[] args)
    {
        // Create a Calendar instance
        Calendar calendar = Calendar.getInstance();
  
        // Set the date you want to modify
        calendar.setTime(new Date());
  
        // Add time (for example, adding 1 hour)
        calendar.add(Calendar.HOUR_OF_DAY, 1);
  
        // Get the modified date
        Date modifiedDate = calendar.getTime();
  
        System.out.println("Original date: " + new Date());
        System.out.println("Modified date: "
                           + modifiedDate);
    }
}


Output

Original date: Wed Feb 28 06:23:48 UTC 2024
Modified date: Wed Feb 28 07:23:48 UTC 2024


Explanation of the above Program:

In the above program is the example of the implementation of the adding time to date in java. In this example, We can create the instance of the calendar then set the time Date() method after that adding one hour after that get the time using calendar instance then print the original date and modified date.

Approach 2: Method to add Time to Date in Java Using java.time

Step-by-Step implementation:

  1. Create the instance of the local date time using LocalDateTime() method and adding import packages.
  2. Set the time using new date then adding one hour.
  3. After that getting the modified time using plusHours() method.
  4. Print the results of the modified time and original time.

Below is the Implementation of add Time to Date in Java Using java.time

Java




// Java Program to add Time to Date in Java Using java.time
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
  
public class JavaAPIExample {
    public static void main(String[] args)
    {
        LocalDateTime localDateTime = LocalDateTime.now();
  
        // Add time (for example, adding 1 hour)
        LocalDateTime modifiedDateTime
            = localDateTime.plusHours(1);
  
        // Convert LocalDateTime to Date
        Date modifiedDate = Date.from(
            modifiedDateTime.atZone(ZoneId.systemDefault())
                .toInstant());
  
        System.out.println("Original date: " + new Date());
        System.out.println("Modified date: "
                           + modifiedDate);
    }
}


Output

Original date: Wed Feb 28 06:39:00 UTC 2024
Modified date: Wed Feb 28 07:39:00 UTC 2024


Explanation of the above Program:

In the above program is the example of the implementation to add time to a date using java.time package. In this example, We can create the instance of the LocalTime using now method to set the present after that add one hour using a method then convert into the date format using atZone takes the parameters of the system defaults converts into the date format.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads