How to Add N minutes to current date using java 8

  • Java 8 providing java java.time package which is having more utility methods related to date and time.
  • LocalDateTime class providing plusMinutes(int minutes) method to add minutes to current date or given date in java.
  • Lets see an example program to add 30 minutes to current date using java 8 LocalDateTime class.



#1: Java Example program to add 30 minutes to current date 

  1. package com.instanceofjava.java8;

  2. import java.time.LocalDateTime;

  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview programs
  6.  * 
  7.  * Description: Add minutes to current date using java 8
  8.  *
  9.  */
  10. public class AddMinutesToDate {

  11. public static void main(String[] args) {
  12. //create data using java 8 LocalDateTime 
  13.     LocalDateTime datetime= LocalDateTime.now();
  14. System.out.println("Before adding minutes to date: "+datetime);
  15.     //add seconds by using plusMinutes(Minutes) method
  16. datetime=datetime.plusMinutes(30);
  17. System.out.println("After adding 30 minutes to date: "+datetime);

  18. }

  19. }



Output:


  1. Before adding minutes to date: 2018-02-05T22:14:33.664
  2. After adding 30 minutes to date: 2018-02-05T22:44:33.664



add minutes to current date java 8


Can we define default methods in functional interfaces in java 8

  • Functional interfaces in java 8 will have single abstract method in it.
  • Check below two pages regarding defining and using functional interfaces using Lamda expressions.
  • Java 8 functional interface with example
  • Implement java 8 functional interface using lambda example program
  • We can define default methods in functional interfaces in java 8.
  • But every functional interface must have single abstract method in it.
  • As we know that default methods should have a implementation as they are not abstract methods.
  • Lets see an example program on defining functional interface with default method.



#1:  Java example program to create functional interface with default method.

  1. package com.instanceofjava.java8;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 functional interfaces
  7.  *
  8.  */
  9. @FunctionalInterface
  10. public interface FunctionalInterfaceExample {
  11.  
  12. void show();
  13.         //default method
  14. default void message() {
  15. System.out.println("hello this is default method in functional interface");
  16. }
  17. }


#2: Java Example program on how to implement functional interface abstract method and use default method.


Can we define default methods in functional interfaces in java 8


Functional interface with multiple methods in java 8

  • Functional interfaces will contains single abstract method with @FunctionalInterface annotation.
  • Check below two posts for defining functional interface and implementing functional interface method using Lamda in java 8.
  • Java 8 functional interface with example
  • Implement java 8 functional interface using lambda example program
  • Now the question is can we declare or define multiple abstract methods in one functional interface in java 8.
  • No. Functional interface should contain only one abstract method so that Lamda will implement it.
  • Lets see what happens if we define multiple abstract methods inside a functional interface
  • Note: all methods inside any interface by default abstract methods.



  1. package com.instanceofjava.java8;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 functional interfaces
  7.  *
  8.  */
  9. @FunctionalInterface
  10. public interface FunctionalInterfaceExample {
  11. //compile time error: Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is
  12. not a functional interface
  13.  
  14. void show();
  15. void calculate();
  16. }


  • If we define multiple abstract methods inside a functional interface it will throw a compile time error.
  • Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is not a functional interface

Implement java 8 functional interface using lambda example program

  • Functional interfaces introduces in java 8 .
  • Functional interfaces will have single abstract method in it.
  • Check here for full details on Java 8 functional interface with example.
  • Functional interfaces will be implemented by java 8 Lamda expressions.
  • Lets see an example program on how can we use functional interfaces or how can we implement functional interfaces using Lamda expression in java 8.
  • Create one functional interface with one abstract method in it.
  • Create a class and implement this abstract method using Lamda Expressions.



#1: Java example program to create functional interface.

functional interface example in java 8 using lamda expressions


#2: Java example program to implement functional interface using Lamda expressions in java 8.

  1. package com.instanceofjava.java8;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 Lamda expressions
  7.  *
  8.  */
  9. public class LamdaExpressions {

  10. public static void main(String[] args) {
  11. FunctionalInterfaceExample obj = ()->{
  12. System.out.println("hello world");
  13. };

  14. obj.show();
  15. }

  16. }

Output:

  1. hello world

Java 8 functional interface with example

  • Java 8 introduced Lamda expressions and functional interfaces.
  • An interface with one abstract method is known as functional interfaces.
  • Functional interfaces in java 8 marked with @FunctionalInterface.
  • These functional interfaces implementation will be provided by Lamda expressions.
  • Default methods in java 8 interfaces are not abstract methods. But we can define  multiple default methods in functional interface.
  • Java 8 functional interface example program


#1: Java Example program to create java 8 functional interface.


  1. package com.instanceofjava.java8;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 functional interfaces
  7.  *
  8.  */
  9. @FunctionalInterface
  10. public interface FunctionalInterfaceExample {

  11. void show();
  12. }


  • Can we create functional interface with @FunctionalInterface annotation without any single abstract method?
  • No. Functional interfaces must have single abstract method inside it.
  • It will throw compile time error : Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is not a functional interface.
  • Lets see an example program on this so that we can conclude it.
  • How to use these functional interfaces we will discuss in next post on Lamda expressions.
  • Implement java 8 functional interface using lambda example program


#2: What will happend if we define functional interface without a single abstract method in it?

functional interface example in java 8

Java 8 date add n seconds to current date example program

  • Java 8 providing java.time.LocalDateTime class.
  • By using plusSeconds() method of  LocalDateTime we can add seconds to date.
  • By Using minusSeconds() method we can substract  seconds from java date.
  • Lets see an example java program to add n seconds to current date and minus / subtract n seconds from java 8 date. 



#1: Java Example program to add n seconds to current date using java 8.

  1. package com.instanceofjava.java8;
  2. import java.time.LocalDateTime;

  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview programs
  6.  * 
  7.  * Description: java seconds to java 8 date
  8.  *
  9.  */
  10. public class addSecondsToDate {

  11. public static void main(String[] args) {
  12. //create data using java 8 LocalDateTime 
  13. LocalDateTime datetime= LocalDateTime.now();
  14. System.out.println("Before: "+datetime);
  15. //add seconds by using plusSeconds(seconds) method
  16. datetime=datetime.plusSeconds(12);
  17. System.out.println("After: "+datetime);
  18. }

  19. }

Output:
  1. 2018-02-03T20:19:25.760
  2. 2018-02-03T20:19:37.760

#2: Java Example program to subtract n seconds to current date using java 8.


java 8 date subtract seconds  example program
Select Menu