Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?

In this article, we will learn how to get Date/Time/Zone fields from ZonedDateTime and Calendar using different methods provided in Java 1.8 version

1. Get Date/Time/Zone fields from ZonedDateTime :

  • Sometimes, we need specific Date/Time/Zone fields from ZonedDateTime like Year or Month or Day or Hour or Minute or Second or Nanosecond or Zone or Offset instead of whole ZonedDateTime
  • To get those fields separately, we can use below listed methods of ZonedDateTime
    • getYear() – get the Year field from ZonedDateTime
    • getMonthValue() – get the month-of-year field from 1 to 12 from ZonedDateTime 
    • getMonth() – get the month-of-year field using the Month enum from ZonedDateTime 
    • getDayOfMonth() – get the day-of-month field from ZonedDateTime 
    • getDayOfWeek() – get the day-of-week field, which is an enum DayOfWeek from ZonedDateTime 
    • getDayOfYear() – get the day-of-year field from ZonedDateTime 
    • getHour() – get the hour-of-day field from ZonedDateTime 
    • getMinute() – get the minute-of-hour field from ZonedDateTime 
    • getSecond() – get the second-of-minute field from ZonedDateTime 
    • getNano() – get the nano-of-second field from ZonedDateTime 
    • getZone() – get the time-zone field from ZonedDateTime such as ‘Europe/Paris’.
    • getOffset() – get the zone offset, such as ‘+01:00’
  • Finally, print above retrieved values to the console

GetDateTimeZoneFieldsFromZonedDateTime.java

package in.bench.resources.java8.zoneddatetime.examples;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class GetDateTimeZoneFieldsFromZonedDateTime {

	public static void main(String[] args) {

		// get current system date/time with Zone
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Current date/time in default zone is = " + zonedDateTime);


		// 1. Date part
		System.out.println("\n1. Date part from ZonedDateTime : \n");


		// 1.1 get YEAR part from current system date
		int year = zonedDateTime.getYear();
		System.out.println("Year is : " + year);


		// 1.2 get MONTH part from current system date
		int month = zonedDateTime.getMonthValue();
		System.out.println("Month is : " + month);


		// 1.3 get MONTH part from current system date
		Month monthInWords = zonedDateTime.getMonth();
		System.out.println("Month in Words is : " + monthInWords);


		// 1.4 get DAY part from current system date
		int day = zonedDateTime.getDayOfMonth();
		System.out.println("Day is : " + day);


		// 1.5 get DAY part from current system date
		DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
		System.out.println("Day of Week is : " + dayOfWeek);


		// 1.6 get DAY part from current system date
		int dayOfYear = zonedDateTime.getDayOfYear();
		System.out.println("Day of Year is : " + dayOfYear);



		// 2. Time part
		System.out.println("\n2. Time part from ZonedDateTime : \n");


		// 2.1 get HOUR value from current system time
		int hours = zonedDateTime.getHour();
		System.out.println("Hour is : " + hours);


		// 2.2 get MINUTE value from current system time
		int minutes = zonedDateTime.getMinute();
		System.out.println("Minutes is : " + minutes);


		// 2.3 get SECOND value from current system time
		int seconds = zonedDateTime.getSecond();
		System.out.println("Seconds is : " + seconds);


		// 2.4 get NANO SECOND value from current system time
		int nano = zonedDateTime.getNano();
		System.out.println("Nanoseconds is : " + nano);



		// 3. Zone part
		System.out.println("\n3. Zone part from ZonedDateTime : \n");


		// 3.1 get ZONE part from current system zone
		ZoneId zoneId = zonedDateTime.getZone();
		System.out.println("Zone is : " + zoneId);


		// 3.2 get OFFSET part from current system zone
		ZoneOffset zoneOffset = zonedDateTime.getOffset();
		System.out.print("Offset  is : " + zoneOffset);
	}
}

Output:

Current date/time in default zone is = 2022-08-11T21:44:33.676816600+05:30[Asia/Calcutta]

1. Date part from ZonedDateTime : 

Year is : 2022
Month is : 8
Month in Words is : AUGUST
Day is : 11
Day of Week is : THURSDAY
Day of Year is : 223

2. Time part from ZonedDateTime : 

Hour is : 21
Minutes is : 44
Seconds is : 33
Nanoseconds is : 676816600

3. Zone part from ZonedDateTime : 

Zone is : Asia/Calcutta
Offset  is : +05:30

2. Get Date/Time/Zone fields from Date/Calendar :

  • Prior to Java 1.8 version, we have to deal with Date and Calendar for Date/Time/Zone handling
  • So if we need specific Date/Time/Zone fields like Year or Month or Day or Hour or Minute or Second or Millisecond or Zone or Offset instead of whole Date, we can use get() method of Calendar passing different constants as listed below,
    • get(Calendar.YEAR) – returns Year field from Calendar
    • get(Calendar.MONTH) – returns Month field from Calendar
    • get(Calendar.DAY_OF_MONTH) – returns Day of Month field from Calendar
    • get(Calendar.DAY_OF_WEEK) – returns Day of Week field from Calendar (Sunday being 1 & Saturday being 7)
    • get(Calendar.DAY_OF_YEAR) – returns Day of Year field from Calendar
    • get(Calendar.HOUR) – returns Hour field from Calendar
    • get(Calendar.MINUTE) – returns Minute field from Calendar
    • get(Calendar.SECOND) – returns Second field from Calendar
    • get(Calendar.MILLISECOND) – returns Millisecond field from Calendar
    • get(Calendar.AM_PM) – returns 0 (AM) or 1 (PM) field from Calendar
    • get(Calendar.ZONE_OFFSET) – returns offset from GMT in milliseconds
  • Finally, print above retrieved values to the console

GetDateTimeZoneFieldsFromCalendar.java

package in.bench.resources.java8.zoneddatetime.examples;

import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class GetDateTimeZoneFieldsFromCalendar {

	public static void main(String[] args) {

		// get current System Date using Date/Calendar
		Date today = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		System.out.println("Current System Date/Time is = " + calendar.getTime());


		// 1. Date part
		System.out.println("\n1. Date fields :-");


		// 1.1 get year
		int year = calendar.get(Calendar.YEAR);
		System.out.println("\nYear is = " + year);


		// 1.2 get month
		int month = calendar.get(Calendar.MONTH);
		System.out.println("Month is = " + month);


		// 1.3 get day of month
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		System.out.println("Day of Month is = " + day);


		// 1.4 get day of week (Sunday=1 and Saturday=7
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		System.out.println("Day of Week is = " + dayOfWeek);


		// 1.5 get day of year
		int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
		System.out.println("Day of Year is = " + dayOfYear);


		// 2. Time part
		System.out.println("\n2. Time fields :-");


		// 2.1 get hour
		int hour = calendar.get(Calendar.HOUR);
		System.out.println("\nHour is = " + hour);


		// 2.2 get minute
		int minute = calendar.get(Calendar.MINUTE);
		System.out.println("Minute is = " + minute);


		// 2.3 get second
		int second = calendar.get(Calendar.SECOND);
		System.out.println("Second is = " + second);


		// 2.4 get millisecond
		int millisecond = calendar.get(Calendar.MILLISECOND);
		System.out.println("MilliSecond is = " + millisecond);


		// 2.5 get AM or PM
		int amOrPm = calendar.get(Calendar.AM_PM);
		System.out.println("AM or PM ? = " + (amOrPm==1?"PM":"AM"));


		// 3. Zone part
		System.out.println("\n3. Zone fields :-");


		// 3.1 get offset in milliseconds
		int offset = calendar.get(Calendar.ZONE_OFFSET);
		System.out.println("\nOffset in Milliseconds = " + offset);


		// 3.2 get offset in difference of GMT hours
		TimeZone timeZone = TimeZone.getDefault();
		String offsetId = timeZone.toZoneId().getRules().getStandardOffset(Instant.now()).getId();
		System.out.print("Offset in Hours = " + offsetId);
	}
}

Output:

Current System Date/Time is = Thu Aug 11 21:41:41 IST 2022

1. Date fields :-

Year is = 2022
Month is = 7
Day of Month is = 11
Day of Week is = 5
Day of Year is = 223

2. Time fields :-

Hour is = 9
Minute is = 41
Second is = 41
MilliSecond is = 177
AM or PM ? = PM

3. Zone fields :-

Offset in Milliseconds = 19800000
Offset in Hours = +05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
Java 8 – What are all the Temporal Units supported by LocalDateTime ?