Javaexercise.com

Java LocalDateTime atZone() Method (Convert LocalDateTime To ZonedDateTime)

Java atZone() method belongs to LocalDateTime class which is stored in java.time package. This method is used to combine date-time with a time-zone and create a ZonedDateTime.

LocalDateTime class represents a date-time without time-zone, so if we wish to get a date-time with time-zone then atZone() method is suitable. The Time zone can be any specific zone area like Asia/Kolkata, Asia/Tokyo, Europe/Paris, etc.

This method takes a single argument of ZoneId type to specify the time-zone and returns a zone-based date-time object(ZonedDateTime). The syntax of atZone() method is the following:

Syntax

public ZonedDateTime atZone(ZoneId zone)

 

Parameters

It takes a single ZoneId type parameter, like 'Asia/Kolkata'.

Return Value

It returns a ZonedDateTime after combining the time-zone to date-time.

 

Example: Get ZonedDateTime From LocalDateTime

Let's take an example to convert date-time to ZonedDateTime. Here we are using atZone() method and specifying system default time-zone. It results in a zone-based date-time. See the example following:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/* 
 * Example to get ZonedDateTime from 
 * LocalDateTime's atZone() method.
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Create a LocalDateTime date
		LocalDateTime datetime = LocalDateTime.parse("2012-05-02T02:30:10");
		// Print Date and time
		System.out.println("Date and Time : "+datetime);
		// Getting Day of week
		ZonedDateTime zdatetime = datetime.atZone(ZoneId.systemDefault());
		// Display zoned date time
		System.out.println("Zoned Date and Time : "+zdatetime);
	}
}

Output:

Date and Time : 2012-05-02T02:30:10
Zoned Date and Time : 2012-05-02T02:30:10+05:30[Asia/Kolkata]
 

Example: Specify Time Zone To atZone() Method

If we want to specify the time-zone rather than setting the system default time-zone, we can use of() method of ZoneId to set the time-zone. See, We set the Tokyo time zone to get date and time based on that area.

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/* 
 * Example to get ZonedDateTime from 
 * LocalDateTime's atZone() method.
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Create a LocalDateTime date
		LocalDateTime datetime = LocalDateTime.parse("2012-05-02T02:30:10");
		// Print Date and time
		System.out.println("Date and Time : "+datetime);
		// Getting Zoned-date-time
		ZonedDateTime zdatetime = datetime.atZone(ZoneId.of("Asia/Tokyo"));
		// Display zoned date time
		System.out.println("Zoned Date and Time : "+zdatetime);
	}
}

Output:

Date and Time : 2012-05-02T02:30:10
Zoned Date and Time : 2012-05-02T02:30:10+09:00[Asia/Tokyo]

 


Conclusion

Well, in this topic, we learnt To combine time-zone with date-time to get ZonedDateTime object. We used several examples to explain the use of the atZone() method and specified zoneId, such as 'Asia/Kolkata'.

If we missed something, you can suggest us at - info.javaexercise@gmail.com