Wednesday, 17 January 2018

How to subtract/add hours, minutes and seconds using minus and plus methods of LocalTime Class


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

LocalTimeDemo1.java

import java.time.LocalTime;

public class LocalTimeDemo1
{

    public static void main(String[] args)
    {
        LocalTime time1 = LocalTime.now();
        System.out.println("time1         = " + time1);
        /*
         * Parameters:
         *
         * hoursToSubtract - the hours to subtract, may be negative
         *
         * Returns:
         *
         * a LocalTime based on this time with the hours subtracted,
         * not null
         */

        LocalTime time2 = time1.minusHours(2);
        System.out.println("Hours Changed = " + time2);

        /*
         * Parameters:
         *
         * minutesToSubtract - the minutes to subtract, may be
         * negative
         *
         * Returns:
         *
         * a LocalTime based on this time with the minutes subtracted,
         * not null
         */

        LocalTime time3 = time1.minusMinutes(30);
        System.out.println("Min Changed   = " + time3);

        /*
         * Parameters:
         *
         * secondsToSubtract - the seconds to subtract, may be
         * negative
         *
         * Returns:
         *
         * a LocalTime based on this time with the seconds subtracted,
         * not null
         */

        LocalTime time4 = time1.minusSeconds(10);
        System.out.println("Sec Changed   = " + time4);
    }

}

Output

time1         = 10:20:19.209
Hours Changed = 08:20:19.209
Min Changed   = 09:50:19.209
Sec Changed   = 10:20:09.209

LocalTimeDemo2.java

import java.time.LocalTime;

public class LocalTimeDemo2
{

    public static void main(String[] args)
    {
        LocalTime time1 = LocalTime.now();
        System.out.println("time1         = " + time1);

        /*
         * Parameters:
         *
         * hoursToAdd - the hours to add, may be negative
         *
         * Returns:
         *
         * a LocalTime based on this time with the hours added, not
         * null
         */

        LocalTime time2 = time1.plusHours(2);
        System.out.println("Hours Changed = " + time2);

        /*
         * Parameters:
         *
         * minutesToAdd - the minutes to add, may be negative
         *
         * Returns:
         *
         * a LocalTime based on this time with the minutes added, not
         * null
         */

        LocalTime time3 = time1.plusMinutes(30);
        System.out.println("Min Changed   = " + time3);

        /*
         * Parameters:
         *
         * secondstoAdd - the seconds to add, may be negative
         *
         * Returns:
         *
         * a LocalTime based on this time with the seconds added, not
         * null
         */

        LocalTime time4 = time1.plusSeconds(10);
        System.out.println("Sec Changed   = " + time4);
    }

}

Output

time1         = 10:20:27.107
Hours Changed = 12:20:27.107
Min Changed   = 10:50:27.107
Sec Changed   = 10:20:37.107

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LocalTimeDemo_minus_plus.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LocalTimeDemo_minus_plus

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/0f51b390145d58b657670d4890d43cef82a5202a/BasicJava/LocalTimeDemo_minus_plus/?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
  • No comments:

    Post a Comment