How to subtract minutes from current time in java



Program #1 : Java Example Program to subtract minutes from given string formatted date time using java 8.


  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract minutes from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubtractMinutesJava{

  11. public static void main(String[] args) {
  12.  
  13.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  14.  
  15.         String currentTime= "2017-10-19 22:00:00";

  16.  
  17.         System.out.println("Before subtraction of minutes from date: "+currentTime);
  18.  
  19.         LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  20.  
  21.         datetime=datetime.minusMinutes(30);
  22.  
  23.         String aftersubtraction=datetime.format(formatter);
  24.         System.out.println("After 30 minutes subtraction from date: "+aftersubtraction);

  25.  
  26.     }
  27.  
  28. }

Output:

  1. Before subtraction of minutes from date: 2017-10-19 22:00:00
  2. After 30 minutes subtraction from date: 2017-10-19 21:30:00

Program #2 : Java Example Program to subtract seconds from given string formatted date time using java 8.
  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract seconds from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubtractSecondsJava{

  11. public static void main(String[] args) {
  12.  
  13.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  14.  
  15.         String currentTime= "2017-10-19 22:00:00";

  16.  
  17.         System.out.println("Before subtraction of seconds from date time: "+currentTime);
  18.  
  19.         LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  20.  
  21.         datetime=datetime.minusSeconds(30);
  22.  
  23.         String aftersubtraction=datetime.format(formatter);
  24.         System.out.println("After 30 seconds subtraction from date time: "+aftersubtraction);

  25.  
  26.     }
  27.  
  28. }

Output:

  1. Before subtraction of seconds from date time: 2017-10-19 22:00:00
  2. After 30 seconds subtraction from date time: 2017-10-19 21:59:30

Program #3 : Java Example Program to subtract seconds from current date time using java 8.


how to subtract minutes from java date time

How to subtract N hours from current date time using java 8

  • We have already seen how to subtract number of days from java using calendar and using java 8.
  • How to subtract X days from a date using Java calendar and with java 8 
  • Now we will discuss here how to subtract hours from java 8 localdatetime.
  • Some times we will get out date time in string format so we will convert our string format date to java 8 LocaDateTime object.
  • We can user DateTimeFormatter to tell the format of date time to LocalDateTime class.
  • LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  • LocalDateTime has minusHours(numberOfHours) method to subtract hours from date time.
  • Lets see an example java program to subtract hours from java date time using java 8 LocalDateTime class.
  • How to subtract hours from java 8 date time.



Program #1 : Java Example Program to subtract hours from given string formatted date time using java 8.



  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract hours from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubstractHoursJava{
  11. public static void main(String[] args) {
  12.  
  13.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  14.  
  15.         String currentTime= "2017-10-19 22:00:00";
  16.  
  17.         System.out.println("Before subtraction of hours from date: "+currentTime);
  18.  
  19.         LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  20.  
  21.         datetime=datetime.minusHours(4);
  22.  
  23.         String aftersubtraction=datetime.format(formatter);
  24.         System.out.println("After 4 hours subtraction from date: "+aftersubtraction);
  25.  
  26.     }
  27.  
  28. }

Output:

  1. Before subtraction of hours from date: 2017-10-19 22:00:00
  2. After 4 hours subtraction from date: 2017-10-19 18:00:00

  • Now we will see how to subtract one hour from current date time in java using java 8 

Program #2 : Java Example Program to subtract one hour from given current date time using java 8.

  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract hours from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubtractOneHour {
  11.  
  12. public static void main(String[] args) {
  13.  
  14.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss");
  15.  
  16.         LocalDateTime datetime = LocalDateTime.now();
  17.         System.out.println("Before subtraction of hours from date: "+datetime.format(formatter));
  18.  
  19.        datetime=datetime.minusHours(1);
  20.        String aftersubtraction=datetime.format(formatter);
  21.        System.out.println("After 1 hour subtraction from date: "+aftersubtraction);
  22.  
  23.     }
  24.  
  25. }
Output:


  1. Before subtraction of hours from date: 2017-10-19 23:14:12
  2. After 1 hour subtraction from date: 2017-10-19 22:14:12

subtract hours from java date time
Select Menu