• 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

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu