Home > Java > javaTutorial > How Do I Get the Current UTC/GMT Date and Time in Java?

How Do I Get the Current UTC/GMT Date and Time in Java?

Mary-Kate Olsen
Release: 2024-12-22 01:59:09
Original
811 people have browsed it

How Do I Get the Current UTC/GMT Date and Time in Java?

How can I get the current date and time in UTC or GMT in Java?

Java 8 introduced the java.time package, which offers a modern and comprehensive set of classes for date and time handling. To retrieve the current date and time in UTC or GMT using this package, simply use the following line of code:

Instant.now()
Copy after login

The Instant class represents a specific moment on the timeline in UTC with nanosecond precision. To convert this instant to a more readable string representation, you can use the toString() method:

Instant.now().toString()
Copy after login

Example Output:

2016-09-13T23:30:52.123Z
Copy after login

This format follows the ISO 8601 standard and includes the time zone designator "Z" to indicate UTC.

Note: Prior to Java 8, the java.util.Date class was commonly used for date and time operations. However, this class has several limitations, including the lack of a明确defined time zone. As a result, it is strongly recommended to use the java.time package for date and time handling in modern Java applications.

The above is the detailed content of How Do I Get the Current UTC/GMT Date and Time in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template