Monday 19 February 2018

How to add hour, min to time using plus method which accepts long and temporal unit of OffsetTime


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

OffsetTimeDemo.java

import java.time.OffsetTime;
import java.time.temporal.ChronoUnit;

public class OffsetTimeDemo
{

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

        /*
         * Parameters:
         *
         * amountToAdd - the amount of the unit to add to the result,
         * may be negative
         *
         * unit - the unit of the amount to add, not null
         *
         * Returns:
         *
         * an OffsetTime based on this time with the specified
         * amount added, not null
         */

        OffsetTime offsetTime2 = offsetTime1.plus(2, ChronoUnit.HOURS);
        System.out.println("offsetTime2           = " + offsetTime2);
       
       
        OffsetTime offsetTime3 = offsetTime2.plus(20, ChronoUnit.MINUTES);
        System.out.println("offsetTime3           = " + offsetTime3);
    }

}

Output

offsetTime1           = 08:16:57.698+05:30
offsetTime2           = 10:16:57.698+05:30
offsetTime3           = 10:36:57.698+05:30

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/6f1f0d2759f060503c7b9e4a4259ae3ddbfe139e/BasicJava/OffsetTimeDemo_plus_long_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
  • No comments:

    Post a Comment