• 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

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