When I create a new Date
object, it is initialized to the current time but in the local timezone. How can I get the current date and time in GMT?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Here is my implementation of toUTC:
There's probably several ways to improve it, but it works for me.
This code prints the current time UTC.
Result
Wrong!
and
will return the same time. Idem for
Just to make this simpler, to create a
Date
inUTC
you can useCalendar
:Which will construct a new instance for
Calendar
using the "UTC"TimeZone
.If you need a
Date
object from that calendar you could just usegetTime()
.