- 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
- package java8;
- /**
- * By: www.instanceofjava.com
- * program: how to subtract minutes from date time using java 8
- *
- */
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- public class SubtractMinutesJava{
- public static void main(String[] args) {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- String currentTime= "2017-10-19 22:00:00";
- System.out.println("Before subtraction of minutes from date: "+currentTime);
- LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
- datetime=datetime.minusMinutes(30);
- String aftersubtraction=datetime.format(formatter);
- System.out.println("After 30 minutes subtraction from date: "+aftersubtraction);
- }
- }
Output:
- Before subtraction of minutes from date: 2017-10-19 22:00:00
- 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.
- package java8;
- /**
- * By: www.instanceofjava.com
- * program: how to subtract seconds from date time using java 8
- *
- */
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- public class SubtractSecondsJava{
- public static void main(String[] args) {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- String currentTime= "2017-10-19 22:00:00";
- System.out.println("Before subtraction of seconds from date time: "+currentTime);
- LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
- datetime=datetime.minusSeconds(30);
- String aftersubtraction=datetime.format(formatter);
- System.out.println("After 30 seconds subtraction from date time: "+aftersubtraction);
- }
- }
Output:
- Before subtraction of seconds from date time: 2017-10-19 22:00:00
- 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.
No comments