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.
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
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