postgresqlHow do I use PostgreSQL ZonedDateTime to store date and time information?
PostgreSQL's ZonedDateTime
type is a convenient way to store date and time information. It is a combination of a LocalDateTime
and a ZoneId
that specifies the timezone associated with the date and time.
For example, to store the date and time for the current moment in the UTC timezone, you can use the following code:
import java.time.ZonedDateTime;
ZonedDateTime nowUtc = ZonedDateTime.now(ZoneId.of("UTC"));
System.out.println(nowUtc);
This will output something like 2020-04-27T22:30:05.142345Z[UTC]
.
You can also use ZonedDateTime
to create a LocalDateTime
object in a specific timezone, or to convert an existing LocalDateTime
object to a different timezone. For example:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
LocalDateTime now = LocalDateTime.now();
ZonedDateTime nowUtc = ZonedDateTime.of(now, ZoneId.of("UTC"));
System.out.println(nowUtc);
This will output something like 2020-04-27T22:30:05.142345Z[UTC]
.
To store a ZonedDateTime
in a PostgreSQL database, you can use the timestamptz
data type. For example:
INSERT INTO my_table (created_at) VALUES (timestamptz '2020-04-27T22:30:05.142345Z[UTC]');
Helpful links
More of Postgresql
- How do I store an array in a PostgreSQL database?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I extract the year from a PostgreSQL timestamp?
- How do I install and configure PostgreSQL on a Windows machine?
- How can I convert XML data to a PostgreSQL table?
- How do I install PostgreSQL and Zabbix on my system?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I use PostgreSQL's UPSERT feature?
- How do I use PostgreSQL with Qt?
See more codes...