Why Does Java’s Date Show an 8‑Hour Shift? Uncovering Timezone Pitfalls

An online promotion that should end at 23:59:59 displayed as finished early due to an 8‑hour discrepancy, traced through HTTP packet capture, DB fields, and Java’s Date handling, revealing that Date objects use the local timezone while timestamps are based on UTC, causing mismatched calculations.

Programmer DD
Programmer DD
Programmer DD
Why Does Java’s Date Show an 8‑Hour Shift? Uncovering Timezone Pitfalls

Online Issue

New feature marketing activity was supposed to end at 23:59:59, but at 16:00 the page already showed "activity ended".

Investigation Process

HTTP packet capture

activityEndTime = 1654761599000, which converts to 2022-06-09 15:59:59, 8 hours earlier than expected. Not a frontend issue.

Database query

Activity end time stored in two fields:

endDate (dateTime): 2022-06-09
endTime (time): 23:59:59

Java receives both as Date, no issue observed. Possibly time type to Date conversion?

Cache inspection endDate=1654704000000, endTime=57599000 Data flow DB → Java Date → JSON serialization is correct.

Thus the only possibility is an error when concatenating endDate and endTime into activityEndTime.

Concatenation logic

activityEndTime = endDate.getTime() + endTime.getTime();

Debug shows this is problematic.

Calling Date.getTime() returns milliseconds since 1970‑01‑01 00:00:00 GMT. For UTC+8, this shifts 8 hours left.

Therefore, when adding endDate (midnight) and endTime (23:59:59) timestamps, the result is offset by an extra 8 hours.

Demo code:

Date date1 = new Date(0L); // 1970‑01‑01 00:00:00 GMT, 08:00 in UTC+8
Date date2 = new Date(3600*1000L); // 01:00 GMT, 09:00 UTC+8
Date date3 = new Date(7200L*1000); // 02:00 GMT, 10:00 UTC+8
Date date4 = new Date(date1.getTime() + date2.getTime());
Date date5 = new Date(date1.getTime() + date2.getTime() + date3.getTime());
System.out.println(date4);
System.out.println(date5);

Problem Cause

The missing 8 hours stem from the fact that the stored date and time are in the local timezone (UTC+8), but getTime() converts them to UTC timestamps, causing a left shift of 8 hours. Adding them together shifts only once to the right, resulting in an overall 8‑hour loss.

4. Date’s Timezone Determination

Java determines the current timezone from the system property user.timezone; if unset, it falls back to native methods based on java.home. Typically the timezone configuration resides in /etc/localtime or the zoneinfo database.

Source: 2021不再有雨, blog.csdn.net/w727655308/article/details/125211726
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingJavaTimezonedateTime Conversion
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.