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.
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