Java LocalDateTime Class | getNano() Method with Example

LocalDateTime Class getNano() method: Here, we are going to learn about the getNano() method of LocalDateTime Class with its syntax and example.
Submitted by Preeti Jain, on June 06, 2020

LocalDateTime Class getNano() method

  • getNano() method is available in java.time package.
  • getNano() method is used to get nano-of-second field value from this date-time object.
  • getNano() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getNano() method does not throw an exception at the time of getting nano of second.

Syntax:

    public int getNano();

Parameter(s):

  • None

Return value:

The return type of this method is int, it returns the value nano-of-second field of this object and the number of nanoseconds in a second starts from 0 and ends at 999,999,999.

Example:

// Java program to demonstrate the example 
// of getNano() method of LocalDateTime

import java.time.*;

public class GetNanoOfLocalDateTime {
    public static void main(String args[]) {
        // Instantiates two LocalDateTime
        LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10.2458");
        LocalDateTime da_ti2 = LocalDateTime.now();

        // Display da_ti1, da_ti2
        System.out.println("LocalDateTime da_ti1 and da_ti2: ");
        System.out.println("da_ti1: " + da_ti1);
        System.out.println("da_ti2: " + da_ti2);

        System.out.println();

        // Here, this method returns the field
        // value nano-of-second in an integer 
        // from this date-time object (da_ti1)
        int get_nano = da_ti1.getNano();

        // Display get_nano
        System.out.print("da_ti1.getNano(): ");
        System.out.println(get_nano);

        // Here, this method returns the field
        // value nano-of-second in an integer 
        // from this date-time object (da_ti2)
        get_nano = da_ti2.getNano();

        // Display get_nano
        System.out.print("da_ti2.getNano(): ");
        System.out.println(get_nano);
    }
}

Output

LocalDateTime da_ti1 and da_ti2: 
da_ti1: 2005-10-05T10:10:10.245800
da_ti2: 2020-06-06T20:38:07.340022

da_ti1.getNano(): 245800000
da_ti2.getNano(): 340022000


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.