Tuesday 16 January 2018

How to use range method of Java 8 LocalDate Class | Java 8 Date and Time


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

LocalDateDemo.java

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

public class LocalDateDemo
{

    public static void main(String[] args)
    {
        LocalDate date = LocalDate.parse("2017-12-03");

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

        ValueRange dayOfMonthRange = date.range(ChronoField.DAY_OF_MONTH);
        System.out.println("dayOfMonthRange = "+dayOfMonthRange);
        System.out.println("Max DayOfMonth = "+ dayOfMonthRange.getMaximum());
        System.out.println("Min DayOfMonth = "+ dayOfMonthRange.getMinimum());
       
        System.out.println("--------------------------");
       
        ValueRange monthOfYearRange = date.range(ChronoField.MONTH_OF_YEAR);
        System.out.println("monthOfYearRange = "+monthOfYearRange);
        System.out.println("Max MonthOfYear = "+ monthOfYearRange.getMaximum());
        System.out.println("Min MonthOfYear = "+ monthOfYearRange.getMinimum());
       
        System.out.println("--------------------------");
       
        ValueRange dayOfYearRange = date.range(ChronoField.DAY_OF_YEAR);
        System.out.println("dayOfYearRange = "+dayOfYearRange);
        System.out.println("Max DayOfYear = "+ dayOfYearRange.getMaximum());
        System.out.println("Min DayOfYear = "+ dayOfYearRange.getMinimum());
       
    }

}

Output

dayOfMonthRange = 1 - 31
Max DayOfMonth = 31
Min DayOfMonth = 1
--------------------------
monthOfYearRange = 1 - 12
Max MonthOfYear = 12
Min MonthOfYear = 1
--------------------------
dayOfYearRange = 1 - 365
Max DayOfYear = 365
Min DayOfYear = 1

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

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

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

    Post a Comment