In this post, we will demonstrate how to get a day, month and year from LocalDateTime in Java.
LocalDateTime class provides below APIs to get a year, month, day from LocalDateTime.
- int getYear() - Gets the year field.
- Month getMonth() - Gets the month-of-year field using the Month enum.
- int getDayOfMonth() - Gets the day-of-month field.
- DayOfWeek getDayOfWeek() - Gets the day-of-week field, which is an enum DayOfWeek.
- int getDayOfYear() - Gets the day-of-year field.
Get Day Month and Year from LocalDateTime in Java
import java.time.LocalDateTime;
/**
* Program to demonstrate LocalDateTime Class APIs.
* @author javaguides.net
*
*/
public class LocalDateTimeExample {
public static void main(String[] args) {
getYearMonthDayFromLocalDateTime();
}
private static void getYearMonthDayFromLocalDateTime() {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("Year : " + dateTime.getYear());
System.out.println("Month : " + dateTime.getMonth().getValue());
System.out.println("Day of Month : " + dateTime.getDayOfMonth());
System.out.println("Day of Week : " + dateTime.getDayOfWeek());
System.out.println("Day of Year : " + dateTime.getDayOfYear());
}
}
Output:
Year : 2018
Month : 8
Day of Month : 10
Day of Week : FRIDAY
Day of Year : 222
Comments
Post a Comment