Signup/Sign In
LAST UPDATED: JUNE 15, 2020

Java LocalDate getLong() Method

This method is used to get the value of a date field as long. A date field can be a day, month, or a year. If we want to get a month of the date as long type, use the getLong() method.

This method takes an argument and returns a long type value. The syntax of the method is given below.

Syntax

public long getLong(TemporalField field)

Parameters:

It takes an argument of TemporalField type.

Return:

It returns a long type value.

Time for an Example

Lets take an example to get day of date as long type. We used getLong() method to get long value and passed ChronoField constant which is returns a long numeric value.

import java.time.LocalDate;
import java.time.temporal.ChronoField; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2015, 10, 21);
		System.out.println(localDate);
		long day = localDate.getLong(ChronoField.DAY_OF_MONTH);
		System.out.println("Day of Month : "+day);
	}
}


2015-10-21
Day of Month : 21

Time for another Example

Lets take another example to understand the use of getLong() method. Here we are getting day of year as long type value from the ChronoField.

import java.time.LocalDate;
import java.time.temporal.ChronoField; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2015, 10, 21);
		System.out.println(localDate);
		long day = localDate.getLong(ChronoField.DAY_OF_YEAR);
		System.out.println("Day of Year : "+day);
	}
}


2015-10-21
Day of Year : 294

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.