How to Get Year Month Day from ZonedDateTime in Java



ZonedDateTime class provides below APIs to get year, month, day from ZonedDateTime class.
  • 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.
  • Month getMonth() - Gets the month-of-year field using the Month enum.
  • int getMonthValue() - Gets the month-of-year field from 1 to 12.
  • ZoneOffset getOffset() - Gets the zone offset, such as '+01:00'.
  • int getYear() - Gets the year field.
import java.time.ZonedDateTime;

/**
 * Program to demonstrate ZonedDateTime Class APIs.
 * @author javaguides.net
 *
 */
public class ZonedDateTimeExample {
 
    public static void main(String[] args) {
        getYearMonthDayfromZonedDateTime();
     }

    private static void getYearMonthDayfromZonedDateTime() {
        ZonedDateTime dateTime = ZonedDateTime.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());
        System.out.println("Zone Id : " + dateTime.getZone());
         System.out.println("Offset : " + dateTime.getOffset());
     }
}
Output:
Year : 2018
Month : 8
Day of Month : 11
Day of Week : SATURDAY
Day of Year : 223
Zone Id : Asia/Calcutta
Offset : +05:30

Comments