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

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


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

Share the post

Java 8 date add n seconds to current date example program

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×