Wednesday, 24 January 2018

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
  • No comments:

    Post a Comment