Signup/Sign In

Java LocalDate get() Method With Examples

This method is used to get value of the specified field from a date. It takes an argument of TemporalField type and returns an integer value.

We can use ChronoField enum to fetch fields from the date, such as: day of month, day of week etc.

Syntax

public int get?(TemporalField field)

Parameters:

field - the field to get, not null

Returns:

the value for the field

Example: Find Day of Month

Lets take an example and get day of month by using the get() method. We are using ChronoFiled to specify fields of date.

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;

public class DateDemo {
public static void main(String[] args){  
		
		// Take a date
	    TemporalField field = ChronoField.DAY_OF_MONTH;
	    TemporalAccessor date = LocalDate.of(2012,06,02);

		// Displaying date
		System.out.println("Date : "+date);
		// get() method
		int val = date.get(field);
		System.out.println(val);

	}
}


Date : 2012-06-02
2

Example: Find Day of Week

Lets find day of a week using the get() method by specifying DAY_OF_WEEK of ChronoField enum.

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;

public class DateDemo {
public static void main(String[] args){  
		
		// Take a date
	    TemporalField field = ChronoField.DAY_OF_WEEK;
	    TemporalAccessor date = LocalDate.of(2012,06,02);

		// Displaying date
		System.out.println("Date : "+date);
		// get() method
		int val = date.get(field);
		System.out.println(val);

	}
}


Date : 2012-06-02
6

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.