Wednesday 28 February 2018

How to use withZone method of Clock class | Java 8 Date and Time


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

ClockDemo.java

import java.time.Clock;
import java.time.ZoneId;

public class ClockDemo
{

    public static void main(String[] args)
    {
        Clock clock1 = Clock.systemDefaultZone();
        System.out.println("clock1 = " + clock1.instant());

        /*
         * Parameters:
         *
         * zone - the time-zone to change to, not null
         *
         * Returns:
         *
         * a clock based on this clock with the specified time-zone,
         * not null
         */

        Clock clock2 = clock1.withZone(ZoneId.systemDefault());
        System.out.println("clock2 = " + clock2.instant());
    }

}

Output

clock1 = 2018-02-13T04:24:13.638Z
clock2 = 2018-02-13T04:24:32.123Z

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f479c67d9d2a797db17daef3132e32f3fda78b25/BasicJava/ClockDemo_withzone/?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
  • How to use tickSeconds method of Clock class | Java 8 Date and Time


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

    ClockDemo.java

    import java.time.Clock;
    import java.time.ZoneId;

    public class ClockDemo
    {

        public static void main(String[] args)
        {
            Clock clock1 = Clock.systemDefaultZone();
            System.out.println("clock1 = " + clock1.instant());

            /*
             * Parameters:
             *
             * zone - the time-zone to use to convert the instant to
             * date-time, not null
             *
             * Returns:
             *
             * a clock that ticks in whole seconds using the specified
             * zone, not null
             */

            Clock clock2 = Clock.tickSeconds(ZoneId.systemDefault());
            System.out.println("clock2 = " + clock2.instant());
        }

    }

    Output

    clock1 = 2018-02-13T04:12:24.569Z
    clock2 = 2018-02-13T04:12:40Z

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/f479c67d9d2a797db17daef3132e32f3fda78b25/BasicJava/ClockDemo_tickseconds/?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
  • Java 8 Clock Class | Java 8 Date and Time | Java Date and Time - Playlist

    How to use tickMinutes method of Clock class | Java 8 Date and Time


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

    ClockDemo.java

    import java.time.Clock;
    import java.time.ZoneId;

    public class ClockDemo
    {

        public static void main(String[] args)
        {
            Clock clock1 = Clock.systemDefaultZone();
            System.out.println("clock1 = " + clock1.instant());

            /*
             * Parameters:
             *
             * zone - the time-zone to use to convert the instant to
             * date-time, not null
             *
             * Returns:
             *
             * a clock that ticks in whole minutes using the
             * specified zone, not null
             */

            Clock clock2 = Clock.tickMinutes(ZoneId.systemDefault());
            System.out.println("clock2 = " + clock2.instant());
        }

    }

    Output

    clock1 = 2018-02-13T04:01:38.294Z
    clock2 = 2018-02-13T04:01:00Z

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/f479c67d9d2a797db17daef3132e32f3fda78b25/BasicJava/ClockDemo_tickminutes/?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
  • Tuesday 27 February 2018

    How to use tick method of Clock class | Java 8 Date and Time


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

    ClockDemo.java

    import java.time.Clock;
    import java.time.Duration;

    public class ClockDemo
    {

        public static void main(String[] args)
        {
            Clock baseClock = Clock.systemUTC();
            System.out.println("instant of baseClock = " + baseClock.instant());

            Duration tickDuration = Duration.ofDays(300);
            System.out.println("tickDuration = " + tickDuration);

            /*
             * Parameters:
             *
             * baseClock - the base clock to base the ticking clock on,
             * not null
             *
             * tickDuration - the duration of each visible tick, not
             * negative, not null
             *
             * Returns:
             *
             * a clock that ticks in whole units of the duration,
             * not null
             */


            Clock clock = Clock.tick(baseClock, tickDuration);

            System.out.println("instant of clock     = " + clock.instant());
        }

    }

    Output

    instant of baseClock = 2018-02-13T03:41:00.886Z
    tickDuration = PT7200H
    instant of clock     = 2017-08-22T00:00:00Z

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/1e79a72b731c44149257d4c3aece211de3cbaa86/BasicJava/ClockDemo_tick_duration/?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
  • How to use offset method of Clock class | Java 8 Date and Time


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

    ClockDemo.java

    import java.time.Clock;
    import java.time.Duration;

    public class ClockDemo
    {

        public static void main(String[] args)
        {
            Clock baseClock = Clock.systemUTC();
            System.out.println("instant of baseClock = " + baseClock.instant());

            Duration duration = Duration.ofHours(6);
            System.out.println("duration = " + duration);

            /*
             * Parameters:
             *
             * baseClock - the base clock to add the duration to, not null
             * offset
             *
             * Duration - the duration to add, not null
             *
             * Returns:
             *
             * a clock based on the base clock with the duration added,
             * not null
             */


            Clock clock = Clock.offset(baseClock, duration);
            System.out.println("instant of clock     = " + clock.instant());
        }

    }

    Output

    instant of baseClock = 2018-02-13T03:22:38.254Z
    duration = PT6H
    instant of clock     = 2018-02-13T09:23:36.572Z

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/1e79a72b731c44149257d4c3aece211de3cbaa86/BasicJava/ClockDemo_offset_duration/?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
  • How to use millis method of Clock class | Java 8 Date and Time


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

    ClockDemo.java

    import java.time.Clock;

    public class ClockDemo
    {

        public static void main(String[] args)
        {
            Clock clock = Clock.systemDefaultZone();
            System.out.println("clock = " + clock);

            /*
             * Returns:
             *
             * the current millisecond instant from this clock, measured
             * from the Java epoch of 1970-01-01T00:00Z (UTC), not null
             */

            long millis = clock.millis();
            System.out.println("Clock millis = " + millis);
        }

    }

    Output

    clock = SystemClock[Asia/Calcutta]
    Clock millis = 1518409712509

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/1e79a72b731c44149257d4c3aece211de3cbaa86/BasicJava/ClockDemo_millis/?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
  • Java 8 Clock Class Introduction | Java 8 Date and Time | Java Date and Time


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

    Click the below Image to Enlarge
    Java 8 Clock Class Introduction | Java 8 Date and Time | Java Date and Time
    ClockDemo1.java

    import java.time.Clock;
    import java.time.ZoneId;

    public class ClockDemo1
    {

        public static void main(String[] args)
        {
            /*
             * Returns:
             *
             * a clock that uses the best available system clock in the
             * default zone, not null
             */

            Clock clock = Clock.systemDefaultZone();
            System.out.println("clock = "+clock);

            /*
             * Returns:
             *
             * the time-zone being used to interpret instants, not null
             */

            ZoneId zoneId = clock.getZone();
            System.out.println("zoneId = "+zoneId);
        }

    }

    Output

    clock = SystemClock[Asia/Calcutta]
    zoneId = Asia/Calcutta

    ClockDemo2.java

    import java.time.Clock;
    import java.time.Instant;

    public class ClockDemo2
    {

        public static void main(String[] args)
        {
            /*
             * Returns:
             *
             * a clock that uses the best available system clock in the
             * UTC zone, not null
             */

            Clock clock = Clock.systemUTC();
            System.out.println("clock = "+clock);
            /*
             * Returns:
             *
             * the current instant from this clock, not null
             */

            Instant instant = clock.instant();     
            System.out.println("instant = "+instant);
        }

    }

    Output

    clock = SystemClock[Z]
    instant = 2018-02-12T04:17:39.294Z

    ClockDemo3.java

    import java.time.Clock;
    import java.time.ZoneId;

    public class ClockDemo3
    {

        public static void main(String[] args)
        {

            ZoneId zoneId = ZoneId.systemDefault();
            System.out.println("zoneId = " + zoneId);

            /*
             * Parameters:
             *
             * zone - the time-zone to use to convert the instant to
             * date-time, not null
             *
             * Returns:
             *
             * a clock that uses the best available system clock in the
             * specified zone, not null
             */

            Clock clock = Clock.system(zoneId);
            System.out.println("Clock = " + clock);
        }

    }

    Output

    zoneId = Asia/Calcutta
    Clock = SystemClock[Asia/Calcutta]

    Refer: 
    https://docs.oracle.com/javase/8/docs/api/index.html?java/time/Clock.html

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/1e79a72b731c44149257d4c3aece211de3cbaa86/BasicJava/ClockDemo_intro/?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
  • How to use ofLocal, ofStrict methods of ZonedDateTime Class | Java 8 Date and Time


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

    ZoneDateTimeDemo1.java

    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo1
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);

            ZoneId zoneId = ZoneId.systemDefault();
            System.out.println(zoneId);

            ZoneOffset zoneOffSet = ZoneOffset.UTC;
            System.out.println(zoneOffSet);

            /*
             * Parameters:
             *
             * localDateTime - the local date-time, not null
             *
             * zone - the time-zone, not null
             *
             * preferredOffset - the zone offset, null if no preference
             *
             * Returns:
             *
             * the zoned date-time, not null
             */

            ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(localDateTime,
                    zoneId, zoneOffSet);
            System.out.println(zonedDateTime);

        }

    }

    Output

    2018-02-11T09:52:38.932
    Asia/Calcutta
    Z
    2018-02-11T09:52:38.932+05:30[Asia/Calcutta]

    ZoneDateTimeDemo2.java

    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo2
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);

            ZoneId zoneId = ZoneId.of("Z");
            System.out.println(zoneId);

            ZoneOffset zoneOffSet = ZoneOffset.UTC;
            System.out.println(zoneOffSet);

            /*
             * Parameters:
             *
             * localDateTime - the local date-time, not null
             *
             * offset - the zone offset, not null
             *
             * zone - the time-zone, not null
             *
             * Returns:
             *
             * the zoned date-time, not null
             *
             */


            ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(localDateTime,
                    zoneOffSet, zoneId);
            System.out.println(zonedDateTime);
        }

    }

    Output

    2018-02-11T09:52:49.746
    Z
    Z
    2018-02-11T09:52:49.746Z

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/1e79a72b731c44149257d4c3aece211de3cbaa86/BasicJava/ZoneDateTimeDemo_ofLocal_strict/?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
  • How to use ofInstant methods of ZonedDateTime Class | Java 8 Date and Time


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

    ZoneDateTimeDemo1.java

    import java.time.Instant;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo1
    {

        public static void main(String[] args)
        {
            Instant instant = Instant.now();
            System.out.println(instant);

            ZoneId zoneId = ZoneId.systemDefault();
            System.out.println(zoneId);

            /*
             *
             * Obtains an instance of ZonedDateTime from an Instant.
             *
             * Parameters:
             *
             * instant - the instant to create the date-time from, not
             * null
             *
             * zone - the time-zone, not null
             *
             * Returns:
             *
             * the zoned date-time, not null
             */

            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
            System.out.println(zonedDateTime);

        }

    }

    Output

    2018-02-11T04:05:47.632Z
    Asia/Calcutta
    2018-02-11T09:35:47.632+05:30[Asia/Calcutta]

    ZoneDateTimeDemo2.java

    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo2
    {

        public static void main(String[] args)
        {

            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);

            ZoneOffset zoneOffset = ZoneOffset.UTC;
            System.out.println(zoneOffset);

            ZoneId zoneId = ZoneId.systemDefault();
            System.out.println(zoneId);

            /*
             * Parameters:
             *
             * localDateTime - the local date-time, not null
             *
             * offset - the zone offset, not null
             *
             * zone - the time-zone, not null
             *
             * Returns:
             *
             * the zoned date-time, not null
             *
             */


            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(localDateTime,
                    zoneOffset, zoneId);
            System.out.println(zonedDateTime);
        }

    }

    Output

    2018-02-11T09:35:59.355
    Z
    Asia/Calcutta
    2018-02-11T15:05:59.355+05:30[Asia/Calcutta]

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/f479c67d9d2a797db17daef3132e32f3fda78b25/BasicJava/ZoneDateTimeDemo_ofInstant_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
  • Friday 23 February 2018

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


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

    ZoneDateTimeDemo1.java

    import java.time.ZoneId;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo1
    {

        public static void main(String[] args)
        {

            /*
             * Parameters:
             *
             * year - the year to represent, from MIN_YEAR to MAX_YEAR
             *
             * month - the month-of-year to represent, from 1 (January) to
             * 12 (December)
             *
             * dayOfMonth - the day-of-month to represent, from 1 to 31
             *
             * hour - the hour-of-day to represent, from 0 to 23
             *
             * minute - the minute-of-hour to represent, from 0 to 59
             *
             * second - the second-of-minute to represent, from 0 to 59
             *
             * nanoOfSecond - the nano-of-second to represent, from 0 to
             * 999,999,999
             *
             * zone - the time-zone, not null
             *
             * Returns: the offset date-time, not null
             *
             */


            ZonedDateTime zonedDateTime = ZonedDateTime.of(2018, 2, 3, 6, 30, 40,
                    50000, ZoneId.systemDefault());
            System.out.println(zonedDateTime);

        }

    }

    Output

    2018-02-03T06:30:40.000050+05:30[Asia/Calcutta]

    ZoneDateTimeDemo2.java

    import java.time.LocalDate;
    import java.time.LocalTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo2
    {

        public static void main(String[] args)
        {

            LocalDate localDate = LocalDate.parse("2017-02-03");
            LocalTime localTime = LocalTime.parse("12:30:30");

            /*
             * Parameters:
             *
             * date - the local date, not null
             *
             * time - the local time, not null
             *
             * zone - the time-zone, not null
             *
             * Returns:
             *
             * the offset date-time, not null
             *
             */

           
            ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime,ZoneId.systemDefault());
            System.out.println(zonedDateTime);

        }

    }

    Output

    2017-02-03T12:30:30+05:30[Asia/Calcutta]

    ZoneDateTimeDemo3.java

    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;

    public class ZoneDateTimeDemo3
    {

        public static void main(String[] args)
        {

            LocalDateTime localDateTime = LocalDateTime.now();

            /*
             * Parameters:
             *
             * localDateTime - the local date-time, not null
             *
             * zone - the time-zone, not null
             *
             * Returns:
             *
             * the zoned date-time, not null
             */

            ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime,
                    ZoneId.systemDefault());
            System.out.println(zonedDateTime);

        }

    }

    Output

    2018-02-09T09:59:31.014+05:30[Asia/Calcutta]

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/1bca0230c22240afa29522b797f3f11c5a905b74/BasicJava/ZoneDateTimeDemo_of_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