Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?

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

Form ZonedDateTime passing LocalDateTime & Zone Fields :

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

FormZonedDateTime3.java

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

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class FormZonedDateTime3 {

	public static void main(String[] args) {

		// 1. LocalDateTime
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("LocalDateTime = \n" + localDateTime);


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


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


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


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


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


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

Output:

LocalDateTime = 
2022-08-12T09:40:39.039560500

ZoneId :- 
Asia/Calcutta

ZonedDateTime :- 
2022-08-12T09:40:39.039560500+05:30[Asia/Calcutta]

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

ZonedDateTime in FormatStyle.MEDIUM :- 
12-Aug-2022, 9:40:39 am

ZonedDateTime in FormatStyle.LONG :- 
12 August 2022 at 9:40:39 am IST

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

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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