Friday 9 March 2018

How to use ofHours, ofHoursMinutes, OfHoursMinutesSeconds, ofTotalSeconds method of ZoneOffset Class


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

ZoneOffsetDemo1.java

import java.time.ZoneOffset;

public class ZoneOffsetDemo1
{

    public static void main(String[] args)
    {

        /*
         * Parameters:
         *
         * hours - the time-zone offset in hours, from -18 to +18
         *
         * Returns:
         *
         * the zone-offset, not null
         */

        ZoneOffset zoneOffset = ZoneOffset.ofHours(4);
        System.out.println("zoneOffset = " + zoneOffset);
     
    }

}

Output

zoneOffset = +04:00

ZoneOffsetDemo2.java

import java.time.ZoneOffset;

public class ZoneOffsetDemo2
{

    public static void main(String[] args)
    {

        /*
         * Parameters:
         *
         * hours - the time-zone offset in hours, from -18 to +18
         *
         * minutes - the time-zone offset in minutes, from 0 to ~+mn~59,
         * sign matches hours
         *
         * Returns:
         *
         * the zone-offset, not null
         */

        ZoneOffset zoneOffset = ZoneOffset.ofHoursMinutes(5, 30);
        System.out.println("zoneOffset = " + zoneOffset);

    }

}

Output

zoneOffset = +05:30

ZoneOffsetDemo3.java

import java.time.ZoneOffset;

public class ZoneOffsetDemo3
{

    public static void main(String[] args)
    {

        /*
         * Parameters:
         *
         * hours - the time-zone offset in hours, from -18 to +18
         *
         * minutes - the time-zone offset in minutes, from 0 to ~+mn~59,
         * sign matches hours
         *
         * seconds - the time-zone offset in seconds, from 0 to ~+mn~59,
         * sign matches hours and minutes
         *
         * Returns:
         *
         * the zone-offset, not null
         */

        ZoneOffset zoneOffset = ZoneOffset.ofHoursMinutesSeconds(5,30,20);
        System.out.println("zoneOffset = " + zoneOffset);

    }

}

Output

zoneOffset = +05:30:20

ZoneOffsetDemo4.java

import java.time.ZoneOffset;

public class ZoneOffsetDemo4
{

    public static void main(String[] args)
    {

        /*
         * Parameters:
         *
         * totalSeconds - the total time-zone offset in seconds, from
         * -64800 to +64800
         *
         * Returns:
         *
         * the zone-offset, not null
         */

        ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(7200);
        System.out.println("zoneOffset = " + zoneOffset);

    }

}

Output

zoneOffset = +02:00

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

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

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