Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set time to epoch time java?

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.

like image 284
Mr.Noob Avatar asked Jul 04 '13 12:07

Mr.Noob


People also ask

How do you convert date to epoch 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.

How to get Unix epoch time in Java?

Since Java 8, it is possible to use Instant and its getEpochSecond to compute the Unix time. long ut2 = System. currentTimeMillis() / 1000L; System.

What is epoch date format Java?

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).


2 Answers

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

like image 199
nachokk Avatar answered Sep 25 '22 01:09

nachokk


As I understand, you only want to store it in some variable? So use Date epoch = new Date(0);

like image 34
TulaGingerbread Avatar answered Sep 21 '22 01:09

TulaGingerbread