Javaexercise.com

How to Get Day of Month in Java LocalDate?

This article contains examples of getting a day of Month from Java local date. Basically, we used the getDayOfMonth() method of the LocalDate class which was added to the Java 8 version. See some interesting examples.

Example to Get Day of Month in Java Local Date

import java.time.LocalDate;
/* 
 *  Code example to Get Day of Month from local date in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// Some Date
		String date = "2015-12-10";
		LocalDate localDate = LocalDate.parse(date);
		System.out.println("Date: "+localDate);
		// Get Day of Month
		int year = localDate.getDayOfMonth();
		System.out.println("Day of Month: "+year);

	}
}

Output:

Date: 2015-12-10
Day of Month: 10
 

Example to Get Day of Month from Current Date in Java

The new() method of the LocalDate class returns the current date. We can use the getDayOfMonth() method to get the day of the month on the current date.

import java.time.LocalDate;
/* 
 *  Code example to Get Day of Month from local date in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// Current Date
		LocalDate localDate = LocalDate.now();
		System.out.println("Date: "+localDate);
		// Get Day of Month
		int year = localDate.getDayOfMonth();
		System.out.println("Day of Month: "+year);

	}
}

Output:

Date: 2021-02-23
Day of Month: 23
 

Related Articles