Wednesday 14 February 2018

How to set hour, min, Sec from time using with methods of OffsetTime Class


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

OffsetTimeDemo.java

import java.time.OffsetTime;

public class OffsetTimeDemo
{

    public static void main(String[] args)
    {

        OffsetTime offsetTime1 = OffsetTime.now();
        System.out.println("offsetTime1           = " + offsetTime1);

        /*
         * Parameters:
         *
         * hour - the hour-of-day to set in the result, from 0 to 23
         *
         * Returns:an OffsetTime based on this time with the requested
         * hour, not null
         */

        OffsetTime offsetTime2 = offsetTime1.withHour(1);
        System.out.println("Hour changed          = " + offsetTime2);

        /*
         * Parameters:
         *
         * minute - the minute-of-hour to set in the result, from 0 to
         * 59
         *
         * Returns:an OffsetTime based on this time with the requested
         * minute, not null
         */

        OffsetTime offsetTime3 = offsetTime2.withMinute(10);
        System.out.println("Min changed           = " + offsetTime3);

        /*
         * Parameters:
         *
         * second - the second-of-minute to set in the result, from 0
         * to 59
         *
         * Returns:an OffsetTime based on this time with the requested
         * second, not null
         */

        OffsetTime offsetTime4 = offsetTime3.withSecond(20);
        System.out.println("Sec changed           = " + offsetTime4);

        /*
         * Parameters:
         *
         * nanoOfSecond - the nano-of-second to set in the result,
         * from 0 to 999,999,999
         *
         * Returns:an OffsetTime based on this time with the requested
         * nanosecond, not null
         */

        OffsetTime offsetTime5 = offsetTime4.withNano(50);
        System.out.println("NanoSec changed       = " + offsetTime5);

    }

}

Output

offsetTime1           = 10:07:26.171+05:30
Hour changed          = 01:07:26.171+05:30
Min changed           = 01:10:26.171+05:30
Sec changed           = 01:10:20.171+05:30
NanoSec changed       = 01:10:20.000000050+05:30

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b457fe74cff2a0c164eab97a4d02e8169811f425/BasicJava/OffsetTimeDemo_with/?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