Thursday 29 March 2018

How to use subtractFrom method of Period Class | Java 8 Date and Time


Click here to watch on Youtube:
https://www.youtube.com/watch?v=c9JNzvZLN2o&list=UUhwKlOVR041tngjerWxVccw

PeriodDemo.java

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.Temporal;

public class PeriodDemo
{

    public static void main(String[] args)
    {

        Period period = Period.ofDays(2);
        System.out.println("period = "+period);
       
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate = "+localDate);
       
        /*
         * Parameters:
         *
         * temporal - the temporal object to adjust, not null
         *
         * Returns:
         *
         * an object of the same type with the adjustment made, not
         * null
         */


        Temporal temporal = period.subtractFrom(localDate);
        System.out.println("temporal = "+temporal);

    }

}

Output

period = P2D
localDate = 2018-03-11
temporal = 2018-03-09

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/PeriodDemo_subtractFrom.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/PeriodDemo_subtractFrom

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_subtractFrom/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment