Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?

In this article, we will learn how to form ZonedDateTime passing LocalDate & LocalTime & Zone fields using ZonedDateTime.of() method provided in Java 1.8 version

Form ZonedDateTime passing LocalDate, LocalTime & Zone Fields :

  • ZonedDateTime.of() method returns ZonedDateTime passing LocalDate (Day/Month/Year) and LocalTime (Hour/Minute/Second/Nanosecond) and Zone (Zone/Offset) fields
  • ZonedDateTime.of(LocalDate, LocalTime, ZoneId) –
    • Obtains an instance of ZonedDateTime from a LocalDate and LocalTime
  • Finally, print ZonedDateTime in different formatted style like SHORT, MEDIUM, LONG & FULL to the console

FormZonedDateTime2.java

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

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class FormZonedDateTime2 {

	public static void main(String[] args) {

		// 1. LocalDate
		LocalDate localDate = LocalDate.now();
		System.out.println("LocalDate :- \n" + localDate);


		// 2. LocalDate
		LocalTime localTime = LocalTime.now();
		System.out.println("\nLocalTime :- \n" + localTime);


		// 3. ZoneId
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nZoneId :- \n" + zoneId);


		// 4. form ZonedDateTime passing LocalDate & LocalTime & ZoneId
		ZonedDateTime zonedDateTime = ZonedDateTime
				.of(localDate, localTime, zoneId);
		System.out.println("\nZonedDateTime :- \n" + zonedDateTime);


		// 4.1 format ZonedDateTime in FormatStyle.SHORT
		String formattedStr1 = zonedDateTime.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
		System.out.println("\nZonedDateTime in FormatStyle.SHORT :- \n" + formattedStr1);


		// 4.2 format ZonedDateTime in FormatStyle.MEDIUM
		String formattedStr2 = zonedDateTime.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
		System.out.println("\nZonedDateTime in FormatStyle.MEDIUM :- \n" + formattedStr2);


		// 4.3 format ZonedDateTime in FormatStyle.LONG
		String formattedStr3 = zonedDateTime.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
		System.out.println("\nZonedDateTime in FormatStyle.LONG :- \n" + formattedStr3);


		// 4.4 format ZonedDateTime in FormatStyle.FULL
		String formattedStr4 = zonedDateTime.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL));
		System.out.print("\nZonedDateTime in FormatStyle.FULL :- \n" + formattedStr4);
	}
}

Output:

LocalDate :- 
2022-08-12

LocalTime :- 
09:33:09.025878

ZoneId :- 
Asia/Calcutta

ZonedDateTime :- 
2022-08-12T09:33:09.025878+05:30[Asia/Calcutta]

ZonedDateTime in FormatStyle.SHORT :- 
12/08/22, 9:33 am

ZonedDateTime in FormatStyle.MEDIUM :- 
12-Aug-2022, 9:33:09 am

ZonedDateTime in FormatStyle.LONG :- 
12 August 2022 at 9:33:09 am IST

ZonedDateTime in FormatStyle.FULL :- 
Friday, 12 August, 2022 at 9:33:09 am India Standard Time

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?