Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to subtract minutes from current time in java

  • We have discussed about how to subtract hours from java 8 using LoclaDateTime class in java.
  • How to subtract N hours from current date time using java 8
  • Now we will see how to subtract minutes from current date time in java.
  • How to subtract minutes from current time in java / How to subtract minutes from time in java.
  • Lets see an example java program which will subtract minutes from java date time
  • 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.




This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

How to subtract minutes from current time in java

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×