Thursday, 29 March 2018

How to use with methods of Period Class | Java 8 Date and Time


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

PeriodDemo.java

import java.time.Period;

public class PeriodDemo
{

    public static void main(String[] args)
    {

        Period period1 = Period.ofMonths(12);
        System.out.println("period1 = " + period1);

        /*
         * Parameters:
         *
         * days - the days to represent, may be negative
         *
         * Returns:
         *
         * a Period based on this period with the requested days, not
         * null
         */


        Period period2 = period1.withDays(20);
        System.out.println("period2[withDays] = " + period2);

        /*
         * Parameters:
         *
         * years - the years to represent, may be negative
         *
         * Returns:
         *
         * a Period based on this period with the requested years, not
         * null
         */

        Period period3 = period2.withYears(2017);
        System.out.println("period3[withYears] = " + period3);
       
        /*
         * Parameters:
         *
         * months - the months to represent, may be negative
         *
         * Returns:
         *
         * a Period based on this period with the requested months,
         * not null
         */

        Period period4 = period3.withMonths(1);
        System.out.println("period4[withMonths]= " + period4);

    }

}

Output

period1 = P12M
period2[withDays] = P12M20D
period3[withYears] = P2017Y12M20D
period4[withMonths]= P2017Y1M20D

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_with_methods/?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
  • How to use toTotalMonths method of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period = Period.ofMonths(11);
            System.out.println("period = " + period);
            /*
             * Returns:
             *
             * the total number of months in the period, may be negative
             */

            long totalMonths = period.toTotalMonths();
            System.out.println("totalMonths = " + totalMonths);

        }

    }

    Output

    period = P11M
    totalMonths = 11

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_toTotalMonths/?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
  • 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
  • How to use normalized method of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period1 = Period.ofMonths(14);
            System.out.println("period1 = " + period1);

            /*
             * Ret urns a copy of this period with the years and months
             * normalized.
             *
             * Returns:
             *
             * a Period based on this period with excess months normalized
             * to years, not null
             */

            Period period2 = period1.normalized();
            System.out.println("period2 = " + period2);

        }

    }

    Output

    period1 = P14M
    period2 = P1Y2M

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

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/PeriodDemo-normalized

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo-normalized/?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
  • How to use negated method of Period Class | Java 8 Date and Time


    Click here to watch on Youtube: 
    https://www.youtube.com/watch?v=PDklNB-6mG0&list=UUhwKlOVR041tngjerWxVccw

    PeriodDemo.java

    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period1 = Period.ofMonths(12);
            System.out.println("period1 = " + period1);
            /*
             * Returns a new instance with each amount in this period
             * negated.
             *
             * This returns a period with each of the years, months and
             * days units individually negated. For example, a period of
             * "2 years, -3 months and 4 days" will be negated to
             * "-2 years, 3 months and -4 days".
             *
             * Returns:
             *
             * a Period based on this period with the amounts negated, not
             * null
             */

            Period period2 = period1.negated();
            System.out.println("period2 = " + period2);

        }

    }

    Output

    period1 = P12M
    period2 = P-12M

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_negated/?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
  • How to use multipliedBy method of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period1 = Period.ofDays(12);
            System.out.println("period1 = "+period1);
           
            /*
             * Parameters:
             *
             * scalar - the scalar to multiply by, not null
             *
             * Returns:
             *
             * a Period based on this period with the amounts multiplied
             * by the scalar, not null
             */


            Period period2 = period1.multipliedBy(10);
            System.out.println("period2 = "+period2);

        }

    }

    Output

    period1 = P12D
    period2 = P120D

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_multipliedBy/?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
  • How to use isNegative and isZero methods of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period = Period.ofMonths(12);
            System.out.println("period = "+period);
           
            /*
             * Returns:
             *
             * true if any unit of this period is negative
             */

            boolean isNegative  = period.isNegative();
            System.out.println("isNegative = "+isNegative);
           
           
            /*
             * Checks if all three units of this period are zero.
             *
             * A zero period has the value zero for the years, months and
             * days units
             *
             */

            boolean isZero = period.isZero();
            System.out.println("isZero = "+isZero);

        }

    }

    Output

    period = P12M
    isNegative = false
    isZero = false

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_isNegative_zero/?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
  • How to use get method of Period Class which accepts TemporalUnit | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;
    import java.time.temporal.ChronoUnit;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period = Period.ofMonths(12);
            System.out.println("period = "+period);

            /*
             * Parameters:
             *
             * unit - the TemporalUnit for which to return the value
             *
             * Returns:
             *
             * the long value of the unit
             */

            long monthValue = period.get(ChronoUnit.MONTHS);

            System.out.println("monthValue = "+monthValue);

        }

    }

    Output

    period = P12M
    monthValue = 12

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_get_unit/?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
  • How to use getChronology method of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;
    import java.time.chrono.IsoChronology;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period = Period.ofMonths(12);
            /*
             * Returns:
             *
             * the ISO chronology, not null
             */

            IsoChronology  isoChronology  = period.getChronology();
            System.out.println(isoChronology.getId());

        }

    }

    Output

    ISO

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_getChronology/?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
  • How to use equal method of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            Period period1 = Period.ofDays(12);
            System.out.println("period1 = " + period1);
           
            Period period2 = Period.ofDays(12);
            System.out.println("period2 = " + period2);
            /*
             * Parameters:
             *
             * obj - the object to check, null returns false
             *
             * Returns:
             *
             * true if this is equal to the other period
             */

            boolean isEqual = period1.equals(period2);
            System.out.println("isEqual = " + isEqual);

        }

    }

    Output

    period1 = P12D
    period2 = P12D
    isEqual = true

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_equals/?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
  • Wednesday, 28 March 2018

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


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

    PeriodDemo.java

    import java.time.Period;
    import java.time.temporal.TemporalAmount;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            TemporalAmount temporalAmount = Period.ofMonths(12);

            /*
             * Obtains an instance of Period from a temporal amount.
             *
             * Parameters:
             *
             * amount - the temporal amount to convert, not null
             *
             * Returns:
             *
             * the equivalent period, not null
             */

            Period period = Period.from(temporalAmount);

            System.out.println(period);

        }

    }

    Output

    P12M

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/PeriodDemo_from/?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
  • How to use between method of Period Class | Java 8 Date and Time


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

    PeriodDemo.java

    import java.time.LocalDate;
    import java.time.Month;
    import java.time.Period;

    public class PeriodDemo
    {

        public static void main(String[] args)
        {

            LocalDate startDate = LocalDate.of(2010, Month.JANUARY, 21);
            System.out.println("startDate = " + startDate);

            LocalDate endDate = LocalDate.of(2025, Month.JANUARY, 21);
            System.out.println("endDate = " + endDate);

            /*
             * Parameters:
             *
             * startDateInclusive - the start date, inclusive, not null
             * endDateExclusive - the end date, exclusive, not null
             *
             * Returns:
             *
             * the period between this date and the end date, not null
             */

            Period period = Period.between(startDate, endDate);
            System.out.println("period = " + period);
        }

    }

    Output

    startDate = 2010-01-21
    endDate = 2025-01-21
    period = P15Y

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/PeriodDemo_between/?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
  • How to use addTo method of Period Class which accepts Temporal | Java 8 Date and Time


    Click here to watch on Youtube: 
    https://www.youtube.com/watch?v=83xTS0MH0so&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.addTo(localDate);
            System.out.println("temporal = "+temporal);
        }

    }

    Output

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

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/PeriodDemo_addTo/?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