Skip to content
Advertisement

Handle daylight savings time and representing UTC in Date object

I have a function which returns the current UTC time in Date object, simple. What we do is find current Instant of UTC, put it in Date object and return. Note: Local Host time here is New York/America.

The problem we are now facing is that Date refuses to store March 13 2:02 AM, since the time doesn’t exist (clocks skip an hour from 2 AM to 3 AM on second Sunday of March in NY), but the same exists in UTC, and we want UTC time.

Is there any way to represent "20220313 02:02:00.000" in java Date object.

This is when New York time(local) is "20220312 21:02.00.000"

JavaScript

Advertisement

Answer

You are currently mixing calls to two very different APIs (old and outdated java.util.Date and classes from java.time, like LocalDateTime). I would stick to the newer API because it makes life a lot easier if you want to express the same instant in two different time zones.

You can still use the LocalDateTime in order to parse the value from the String and then add a ZoneId to make it represent a real moment in time.

Here’s an example:

JavaScript

As you can see in the output, the hours of day are different and so is the zone:

JavaScript

If you have to produce/consume java.util.Dates, you can make use of compatibility methods that have been implemented for reasons like yours: Dealing with a considerable large amount of legacy code.

Short: A java.time.ZonedDateTime and a java.time.OffsetDateTime are representations of moments in time, instants. Fortunately, theres a java.time.Instant, too, and you can convert a java.util.Datefrom/to anInstant. There's Date.from(Instant)andDate.toInstant(). Here's how you would use the results of the code example above in order to have the values as Date`:

JavaScript

These lines would produce the following output:

JavaScript

Please have a closer look at the values of the java.util.Dates.
Zones implicitly changed and values adjusted (even though a Date does not really have a zone). You have basically no real control over zone shifts and time conversions.
There are several reasons for a totally new and different datetime API introduced in Java 8… The mentioned is just one of them.


Is there any way to represent “20220313 02:02:00.000” in java Date object?

Yes, there is… You could create the Date and return it. How this Date instance will be represented as String depends on the TimeZone used. See this:

JavaScript

The output is the following, mind that the same Date is used:

JavaScript

OK, the format is not stored in the Date variable, but the underlying value is, at least, which makes it representable in different formats that have different time zones.

Thing to think about:
Do you think the time zone should be part of the format rather than part of a datetime object itself? The answer to this question could be mapped to the question Do you want to use java.util.Date or java.time? 😉

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement