Home >Java >javaTutorial >How Do I Get the Current UTC/GMT Date and Time 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.
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!