Why does subtracting two millisecond timestamps (year 1927) give a strange result?
Hello everyone! I'm a beginner in Java, learning about dates and time. I wrote a simple code that calculates the difference between two timestamps in milliseconds. But the result shocked me!\n\nHere's my code:\n\n```java\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.TimeZone;\n\npublic class StrangeDateDifference {\n public static void main(String[] args) throws Exception {\n SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");\n sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));\n\n Date date1 = sdf.parse("1927-12-31 23:54:07");\n Date date2 = sdf.parse("1927-12-31 23:54:08");\n\n long diffInMillis = date2.getTime() - date1.getTime();\n System.out.println("Difference in milliseconds: " + diffInMillis);\n }\n}\n```\n\nThe result is 353 milliseconds instead of the expected 1000 milliseconds! Why is this happening?
No answers yet.
Log in to leave an answer
Blogs
SOLID Principles in Kotlin: Theory and Practice from A to Z
WebSocket in Java: Real-Time Applications from A to Z
Domain-Driven Design in JavaScript: Complex Code? Let's Organize It Like a Pro
Time Zones in C++: How to Stop Getting Confused About Time Zones