Is it possible to create a java.util.Date
object that is guaranteed to be UTC and at a specific time in the past, like an hour ago or a day ago, and is this a good idea?
I have reviewed the Stack Overflow question
get date as of 4 hours ago and its answers. But I want to avoid having to add more dependencies, like jodaTime
, and the accepted answer uses System.currentTimeMillis()
which would be the local timezone, would it not?
You can achieve this using the
java.time
package, as follows:Gives the following output:
Which is correctly
4+5:30
hours behind my current time -Asia/Kolkata ZoneId
.As discussed vividly in the comments, the recommendation is to use the
java.time
package. The easy solution:This just printed
Since UTC time is now
18:58
, the output is what you asked for. TheInstant
itself is offset neutral. ItstoString
delivers time in UTC, but there was no mention of UTC in producing theInstant
, so whether it gives you what you want, I am not sure. I will give you a result that is explicitly in UTC later.But first, if you do need a
java.util.Date
, typically for a legacy API that you cannot change, the conversion is easy:On my computer in
Europe/Copenhagen
time zone this printed:Again, this agrees with the time four hours before running the snippet. And again, a
Date
doesn’t have a UTC offset in it. Only itstoString
method grabs my JVM’s time zone setting and uses it for generating the string, this does not affect theDate
. See the Stack Overflow question, How to set time zone of a java.util.Date?, and its answers.As promised, if you do need to represent not only the time but also the offset, use an
OffsetDateTime
:This printed
Z
at the end means offset zero from UTC or “Zulu time zone” (which isn’t a true time zone). The conversion to aDate
is not much more complicated than before, but again, you will lose the offset information in the conversion:This printed:
Link: Oracle tutorial explaining how to use
java.time