How to Retrieve and Convert Blockchain Block Timestamps in Java
This guide explains that blockchain block creation times are stored as Unix timestamps in seconds, describes the Unix time concept, shows how to convert timestamps in Java from milliseconds to seconds and back, and provides a concrete Ethereum example using Web3.js to fetch a block’s timestamp.
Block Timestamp Retrieval
Both Bitcoin and Ethereum blocks store their creation time as a Unix timestamp measured in seconds, not milliseconds, so using the raw value without conversion will produce incorrect dates.
Unix Timestamp Overview
A Unix timestamp (POSIX time) counts the total seconds elapsed since 00:00:00 UTC on 1 January 1970.
Conversion in Java
In Java, new Date().getTime() or System.currentTimeMillis() returns the current time in milliseconds. To obtain a Unix‑second timestamp, divide by 1000, e.g., System.currentTimeMillis() / 1000. Conversely, a Unix timestamp can be turned into a Java Date with new java.util.Date(unixTimestamp * 1000).
Example with Ethereum
Ethereum block data often includes a field like:
timestamp: Number - the unix timestamp for when the block was collated.
Using Web3.js you can fetch the block number and its timestamp:
web3.eth.getTransaction("0x5da2844afb6826d4baed6ad7e8b536c00cbc921ac147773ad056f29f2e7c1762").blockNumber
1920050
web3.eth.getBlock(1920050).timestamp
1469021581Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
