Introduction
In this post we will learn how to create ZonedDatetime instance in various ways.
ZonedDatetime class is a part of java.time
package.
You will find yourself using this class most of the time in your project.
ZonedDatetime class is designed to represent date and time with a timezone.
Creating ZonedDatetime Instance
They are multiple ways for creating ZonedDatetime instance based on your requirement.
Let us explore one by one with simple examples.
ZonedDateTime.now()
This is default way of creating ZonedDateTime instance.
To create an ZonedDatetime instance you can use ZonedDatetime.now()
method to obtain current timestamp with default timezone.
Let us check with an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
}
}
|
In the below output my default timezone is chosen since I reside in India.
1
|
2023-11-25T21:19:51.023717+05:30[Asia/Calcutta]
|
ZonedDateTime.now(ZoneId)
In this method you can get current ZonedDateTime based on ZonedId provided by you.
Ensure ZonedDateTime provided by you is in correct format
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTimeEurope = ZonedDateTime.now(ZoneId.of("Europe/London"));
System.out.println(zonedDateTimeEurope);
}
}
|
1
|
2023-11-25T15:49:51.027715800Z[Europe/London]
|
ZonedDateTime.of(LocalDateTime,ZoneId):
Obtains an instance of ZonedDateTime from a local date-time.
Here you need to provide Zone Id to get ZonedDateTime from LocalDateTime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTimeFromLocal = ZonedDateTime.of(LocalDateTime.now(),ZoneId.systemDefault());
System.out.println(zonedDateTimeFromLocal);
}
}
|
1
|
2023-11-25T21:19:51.031495900+05:30[Asia/Calcutta]
|
ZonedDateTime.of(LocalDate,LocalTime,ZoneId):
Obtains an instance of ZonedDateTime from a local date and time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTimeFromLocalDateandLocalTime = ZonedDateTime.of(LocalDate.now(),LocalTime.now(),ZoneId.systemDefault());
System.out.println(zonedDateTimeFromLocalDateandLocalTime);
}
}
|
1
|
2023-11-25T21:19:51.032516700+05:30[Asia/Calcutta]
|
ZonedDateTime.of(year,month,day,hour,minute,second,nanosecond,zoneid):
Obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTimeGranularWay=
ZonedDateTime.of(2023,8,10,10,10,20,20,ZoneId.systemDefault());
System.out.println(zonedDateTimeGranularWay);
}
}
|
1
|
2023-08-10T10:10:20.000000020+05:30[Asia/Calcutta]
|
ZonedDateTime.ofInstant(Instant,ZoneId):
This method can be used to get ZonedDateTime from an Instant
class which we briefly discussed earlier.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTimeFromInstant=ZonedDateTime.
ofInstant(Instant.now(),ZoneId.of("Europe/London"));
System.out.println(zonedDateTimeFromInstant);
}
}
|
1
|
2023-11-25T15:49:51.032516700Z[Europe/London]
|
Putting it altogether
Find complete code here for your reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import java.time.*;
public class ZonedDateTimeDemo {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
ZonedDateTime zonedDateTimeEurope = ZonedDateTime.now(ZoneId.of("Europe/London"));
System.out.println(zonedDateTimeEurope);
ZonedDateTime zonedDateTimeFromLocal = ZonedDateTime.of(LocalDateTime.now(),ZoneId.systemDefault());
System.out.println(zonedDateTimeFromLocal);
ZonedDateTime zonedDateTimeFromLocalEurope = ZonedDateTime.of(LocalDateTime.now(),ZoneId.of("Europe/London"));
System.out.println(zonedDateTimeFromLocalEurope);
ZonedDateTime zonedDateTimeFromLocalDateandLocalTime = ZonedDateTime.of(LocalDate.now(),LocalTime.now(),ZoneId.systemDefault());
System.out.println(zonedDateTimeFromLocalDateandLocalTime);
ZonedDateTime zonedDateTimeGranularWay=
ZonedDateTime.of(2023,8,10,10,10,20,20,ZoneId.systemDefault());
System.out.println(zonedDateTimeGranularWay);
ZonedDateTime zonedDateTimeFromInstant=ZonedDateTime.ofInstant(Instant.now(),ZoneId.of("Europe/London"));
System.out.println(zonedDateTimeFromInstant);
}
}
|
Output is here:
1
2
3
4
5
6
7
|
2023-11-25T21:19:51.023717+05:30[Asia/Calcutta]
2023-11-25T15:49:51.027715800Z[Europe/London]
2023-11-25T21:19:51.031495900+05:30[Asia/Calcutta]
2023-11-25T21:19:51.032516700Z[Europe/London]
2023-11-25T21:19:51.032516700+05:30[Asia/Calcutta]
2023-08-10T10:10:20.000000020+05:30[Asia/Calcutta]
2023-11-25T15:49:51.032516700Z[Europe/London]
|
Conclusion:
If you know above examples you should be good enough to create ZonedDateTime
instance in your project without any problems.