"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Posted on 2025-02-07
Browse:663

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()

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()

Example Output:

2016-09-13T23:30:52.123Z

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3