To add minutes to date and time, you can use the plusMinutes() method of Java 8.
This article introduces how to add minutes to local date time in Java.
LocalDatetime is a class in java.time package that represents local date and time without timezone. It was added to Java 1.8 version.
We can use this method to add minutes such as 1 minute, 5 minutes, 10 minutes, 30, minutes, etc to date and current date as well.
Let's get started.
If you have a date in String then first parse it using the parse() method to get the localdatetime object.
After that use the plusMinutes() method to add minutes to it.
Here, we are adding 10 minutes to the date-time.
/*
* Code example to add minutes to date and time in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// current date time
LocalDateTime date = LocalDateTime.now();
// Displaying date and time
System.out.println("Date : "+date);
// Add 10 minutes to the date
LocalDateTime newDate = date.plusMinutes(10);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T18:03:34.172670353
New Date : 2022-03-14T18:13:34.172670353
If you already have localdatetime object then no need to use the parse() method, just use the plusMinutes() method directly.
Now, let's have a look at this method signature:
public LocalDateTime plusMinutes(long minutes)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of this LocalDateTime with the specified number of minutes added, not null.
Parameters: It takes a single parameter of a long type. It may be negative.
Exceptions: It throws DateTimeException if the result exceeds the date range.
Since: Java 1.8
Here, we have a string date and want to add minutes to it.
For that, first, we used the parse() method to get a localdatetime object and then used plusMinutes() method to add 10 minutes to the date..
package javaexample;
/*
* Code example to add minutes to date and time in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:28:13.048999208";
// parse the date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Add 10 minutes to the date
LocalDateTime newDate = date.plusMinutes(10);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-14T17:38:13.048999208
The plusMinutes() method also allows the negative argument to add minutes. But it does the subtraction actually rather than adding minutes.
We can use this method to subtract minutes as well by passing negative values. See the below code.
Here, we are subtracting 10 minutes from the date-time in java.
package javaexample;
/*
* Code example to add minutes to date and time in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:28:13.048999208";
// parse the date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Add 10 minutes to the date
LocalDateTime newDate = date.plusMinutes(-10);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New Date : 2022-03-14T17:18:13.048999208