Wednesday, 31 January 2018

How to use format method of MonthDay Class | Java 8 Date and Time


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

MonthDayDemo.java

import java.time.MonthDay;
import java.time.format.DateTimeFormatter;

public class MonthDayDemo
{

    public static void main(String[] args)
    {
        MonthDay monthDay = MonthDay.parse("--12-30");
        System.out.println(monthDay);

        /*
         * Parameters:
         *
         * pattern - the pattern to use, not null
         *
         * Returns:
         *
         * the formatter based on the pattern, not null
         *
         */

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("--MM");

        /*
         * Parameters:
         *
         * formatter - the formatter to use, not null
         *
         * Returns:the formatted month-day string, not null
         */


        String formatedValue = monthDay.format(formatter);
        System.out.println(formatedValue);
    }

}

Output

--12-30
--12

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/0341b02de53fb9c08ccad77af304766fda93a105/BasicJava/MonthDayDemo_format/?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 get day and month using getDayOfMonth, getMonth methods of MonthDay Class


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

    MonthDayDemo.java

    import java.time.Month;
    import java.time.MonthDay;

    public class MonthDayDemo
    {

        public static void main(String[] args)
        {
            /*
             * Returns:the current month-day using the system clock and
             * default time-zone, not null
             */

            MonthDay monthDay = MonthDay.now();
            System.out.println("monthDay = " + monthDay);

            /*
             * Returns:the day-of-month, from 1 to 31
             */

            int dayOfTheMonth = monthDay.getDayOfMonth();
            System.out.println("dayOfTheMonth = " + dayOfTheMonth);

            /*
             * Returns:the month-of-year, from 1 to 12
             */

            int monthValue = monthDay.getMonthValue();
            System.out.println("monthValue = " + monthValue);

            /*
             * Returns:the month-of-year, not null
             */

            Month month = monthDay.getMonth();
            System.out.println("month = " + month);
            System.out.println("month value = " + month.getValue());

        }

    }

    Output

    monthDay = --01-18
    dayOfTheMonth = 18
    monthValue = 1
    month = JANUARY
    month value = 1

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/0341b02de53fb9c08ccad77af304766fda93a105/BasicJava/MonthDayDemo_getmonth_day/?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 form LocalDate using atYear method of MonthDay Class | Java 8 Date and Time

    MonthDayDemo.java

    import java.time.LocalDate;
    import java.time.MonthDay;

    public class MonthDayDemo
    {

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

            /*
             * Parameters:
             *
             * year - the year to use, from MIN_YEAR to MAX_YEAR
             *
             * Returns:
             *
             * the local date formed from this month-day and the
             * specified year, not null
             */

            LocalDate localDate = monthDay.atYear(2013);
            System.out.println(localDate);
        }
    }

    Output

    --01-18
    2013-01-18

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/8fa516d67356209c67bf6743dfb7e718c4c9e5f6/BasicJava/MonthDayDemo_atYear/?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 MonthDay Class Introduction | Java 8 Date and Time | Java Date and Time


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

    Click the below Image to Enlarge

    Java 8 MonthDay Class Introduction | Java 8 Date and Time | Java Date and Time

    MonthDayDemo.java

    import java.time.MonthDay;

    public class MonthDayDemo
    {

        public static void main(String[] args)
        {

            /*
             * Returns:
             *
             * the current month-day using the system clock and
             * default time-zone, not null
             */

            MonthDay currentMonthDay = MonthDay.now();
            System.out.println(currentMonthDay);

            /*
             * Parameters:
             *
             * text - the text to parse such as "--12-03", not null
             *
             * Returns:
             *
             * the parsed month-day, not null
             */


            MonthDay monthDay = MonthDay.parse("--03-30");
            System.out.println(monthDay);
        }

    }

    Output

    --01-18
    --03-30

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

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/8fa516d67356209c67bf6743dfb7e718c4c9e5f6/BasicJava/MonthDayDemo_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 format method which accepts DateTimeFormatter | LocalDateTime Class


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

    LocalDateTimeDemo.java

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;

    public class LocalDateTimeDemo
    {

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

            /*
             * Parameters:
             *
             * pattern - the pattern to use, not null
             *
             * Returns:
             *
             * the formatter based on the pattern, not null
             *
             */


            DateTimeFormatter dateTimeFormatter = DateTimeFormatter
                    .ofPattern("dd-MM-yyyy HH:mm:ss");

            /*
             * Parameters:
             *
             * formatter - the formatter to use, not null
             *
             * Returns:
             *
             * the formatted date-time string, not null
             *
             */

            String formatDateTime = localDateTime.format(dateTimeFormatter);
            System.out.println("After Formatting  = " + formatDateTime);
        }

    }

    Output

    Before Formatting = 2018-01-17T10:09:54.188
    After Formatting  = 17-01-2018 10:09:54

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/8fa516d67356209c67bf6743dfb7e718c4c9e5f6/BasicJava/LocalDateTimeDemo_format_datetimeformatter/?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
  • Wednesday, 24 January 2018

    How to use truncatedTo method of LocalDateTime Class | Java 8 Date and Time


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

    LocalDateTimeDemo.java

    import java.time.LocalDateTime;
    import java.time.temporal.ChronoUnit;

    public class LocalDateTimeDemo
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime1 = LocalDateTime.now();
            System.out.println(localDateTime1);
           
            /*
             * Parameters:
             *
             * unit - the unit to truncate to, not null
             *
             * Returns:
             *
             * a LocalDateTime based on this date-time with the
             * time truncated, not null
             */

            LocalDateTime localDateTime2 = localDateTime1
                    .truncatedTo(ChronoUnit.DAYS);
           
            System.out.println(localDateTime2);

        }

    }

    Output

    2018-01-17T09:54:07.362
    2018-01-17T00:00

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_truncatedto/?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 convert LocalDateTime to LocalDate and LocalTime | LocalDateTime Class


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

    LocalDateTimeDemo.java

    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.LocalTime;

    public class LocalDateTimeDemo
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println("localDateTime = " + localDateTime);
           
           
            /*
             * Returns the date part of this date-time, not null.
             */

            LocalDate localDate = localDateTime.toLocalDate();
            System.out.println("localDate = " + localDate);

            /*
             * Returns the time part of this date-time, not null.
             */

            LocalTime localTime = localDateTime.toLocalTime();
            System.out.println("localTime = " + localTime);

        }

    }

    Output

    localDateTime = 2018-01-17T09:41:13.709
    localDate = 2018-01-17
    localTime = 09:41:13.709

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_toLocaldate_time/?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 range method of LocalDateTime Class | Java 8 Date and Time


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

    LocalDateTimeDemo.java

    import java.time.LocalDateTime;
    import java.time.temporal.ChronoField;
    import java.time.temporal.ValueRange;

    public class LocalDateTimeDemo
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime = LocalDateTime
                    .parse("2018-12-03T10:15:30");

            /*
             * Parameters:
             *
             * field - the field to query the range for, not null
             *
             * Returns:
             *
             * the range of valid values for the field, not null
             */

            ValueRange valueRange = localDateTime.range(ChronoField.DAY_OF_YEAR);

            System.out.println("Range : " + valueRange);
            System.out.println("Min : " + valueRange.getMinimum());
            System.out.println("Max : " + valueRange.getMaximum());

            System.out.println("--------------------------------");

            valueRange = localDateTime.range(ChronoField.MONTH_OF_YEAR);

            System.out.println("Range : " + valueRange);
            System.out.println("Min : " + valueRange.getMinimum());
            System.out.println("Max : " + valueRange.getMaximum());
        }

    }
    Output

    Range : 1 - 365
    Min : 1
    Max : 365
    --------------------------------
    Range : 1 - 12
    Min : 1
    Max : 12

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_range/?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 method of LocalDateTime Class | Java 8 Date and Tim


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

    LocalDateTimeDemo.java

    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;

    public class LocalDateTimeDemo
    {

        public static void main(String[] args)
        {

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

            ZoneId zoneId = ZoneId.systemDefault();
            System.out.println("zoneId = "+zoneId);
            /*
             * Parameters:
             *
             * instant - the instant to create the date-time from, not
             * null
             *
             * zone - the time-zone, which may be an offset, not null
             *
             * Returns:the local date-time, not null
             */

            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
            System.out.println("localDateTime = "+localDateTime);
        }

    }

    Output

    instant = 2018-01-16T04:49:52.570Z
    zoneId = Asia/Calcutta
    localDateTime = 2018-01-16T10:19:52.570

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_ofInstant/?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 convert LocalDateTime to OffsetDateTime using atOffset method of LocalDateTime Class


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

    LocalDateTimeDemo.java

    import java.time.LocalDateTime;
    import java.time.OffsetDateTime;
    import java.time.ZoneOffset;

    public class LocalDateTimeDemo
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime = LocalDateTime
                    .parse("2017-02-03T12:30:30");
            System.out.println("localDateTime   = "+localDateTime);

            ZoneOffset zoneOffset = ZoneOffset.ofHours(5);
            System.out.println("zoneOffset      = "+zoneOffset);
            /*
             * Parameters:
             *
             * offset - the offset to combine with, not null
             *
             * Returns:
             *
             * the offset date-time formed from this date-time and the
             * specified offset, not null
             */

            OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffset);
            System.out.println("offsetDateTime  = "+offsetDateTime);
        }

    }

    Output

    localDateTime   = 2017-02-03T12:30:30
    zoneOffset      = +05:00
    offsetDateTime  = 2017-02-03T12:30:30+05:00

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_atOffset/?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 plus, minus methods which accept long and temporal unit | LocalDateTime Class


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

    LocalDateTimeDemo.java

    import java.time.LocalDateTime;
    import java.time.temporal.ChronoUnit;

    public class LocalDateTimeDemo
    {

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

            /*
             * 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:
             *
             * a LocalDateTime based on this date-time with the specified
             * amount added, not null
             */

            LocalDateTime localDateTime2 = currentDatetime.plus(5,
                    ChronoUnit.MONTHS);
            System.out.println("Month Added    = " + localDateTime2);

            /*
             * Parameters:
             *
             * amountToSubtract - the amount of the unit to subtract from
             * the result, may be negative
             *
             * unit - the unit of the amount to subtract, not null
             *
             * Returns:
             *
             * a LocalDateTime based on this date-time with the specified
             * amount subtracted, not null
             */


            LocalDateTime localDateTime3 = currentDatetime.minus(5,
                    ChronoUnit.YEARS);
            System.out.println("Years reduced  = " + localDateTime3);
        }

    }

    Output

    currentDatetime= 2018-01-16T09:44:12.673
    Month Added    = 2018-06-16T09:44:12.673
    Years reduced  = 2013-01-16T09:44:12.673

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_plus_minus_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
  • How to check DateTime is before or after or equals to another DateTime | LocalDateTime Class


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

    LocalDateTimeDemo.java

    import java.time.LocalDateTime;

    public class LocalDateTimeDemo
    {

        public static void main(String[] args)
        {
            LocalDateTime localDateTime1 = LocalDateTime
                    .parse("2016-02-03T12:30:30");
            System.out.println("localDateTime1 = "+localDateTime1);
         
            LocalDateTime localDateTime2 = LocalDateTime
                    .parse("2017-03-03T12:30:30");
             
            System.out.println("localDateTime2 = "+localDateTime2);
         
            /*
             * Returns:true if this is equal to the other date-time
             */

            System.out.println(localDateTime1.equals(localDateTime2));

            /*
             * Returns:true if this date-time is after the specified
             * date-time
             */

            System.out.println(localDateTime1.isAfter(localDateTime2));

            /*
             * Returns:true if this date-time is before the specified
             * date-time
             */


            System.out.println(localDateTime1.isBefore(localDateTime2));
        }

    }

    Output

    localDateTime1 = 2016-02-03T12:30:30
    localDateTime2 = 2017-03-03T12:30:30
    false
    false
    true

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

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

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