Java 8 – How to get Date, Time and Offset fields from OffsetDateTime ?

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

1. Get Date/Time/Offset fields from OffsetDateTime :

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

GetDateTimeOffsetFieldsFromOffsetDateTime.java

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

import java.time.DayOfWeek;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class GetDateTimeOffsetFieldsFromOffsetDateTime {

	public static void main(String[] args) {

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


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


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


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


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


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


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


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



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


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


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


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


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



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


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

Output:

Current date/time in default zone is = 2022-08-16T12:55:01.802876400+05:30

1. Date part from OffsetDateTime : 

Year is : 2022
Month is : 8
Month in Words is : AUGUST
Day is : 16
Day of Week is : TUESDAY
Day of Year is : 228

2. Time part from OffsetDateTime : 

Hour is : 12
Minutes is : 55
Seconds is : 1
Nano Seconds is : 802876400

3. ZoneOffset part from OffsetDateTime : 

Offset  is : +05:30

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

  • Prior to Java 1.8 version, we have to deal with Date and Calendar for Date/Time/Offset 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

GetDateTimeOffsetFieldsFromCalendar.java

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

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

public class GetDateTimeOffsetFieldsFromCalendar {

	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. Offset 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 = Tue Aug 16 12:56:37 IST 2022

1. Date fields :-

Year is = 2022
Month is = 7
Day of Month is = 16
Day of Week is = 3
Day of Year is = 228

2. Time fields :-

Hour is = 0
Minute is = 56
Second is = 37
MilliSecond is = 757
AM or PM ? = PM

3. Offset fields :-

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

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to form OffsetDateTime passing Date, Time and Offset fields ?
Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?