I'm trying to set the time to epoch date time in java. how can I do this? so that I could get year months days etc out of the epoch date time.
Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.
Since Java 8, it is possible to use Instant and its getEpochSecond to compute the Unix time. long ut2 = System. currentTimeMillis() / 1000L; System.
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
use new Date(0L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(new Date(0L)));
Take care of your timezone cause it will change depends on what you have by default.
UPDATE
In java 8 you can use the new java.time
library
You have this constant Instant.EPOCH
As I understand, you only want to store it in some variable? So use
Date epoch = new Date(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With