98

I can convert a unix timestamp to a Date() object by putting the long value into the Date() constructor. For eg: I could have it as new Date(1318762128031).

But after that, how can I get back the unix timestamp from the Date() object?

awhie29urh2
  • 15,547
  • 2
  • 19
  • 20
Carven
  • 14,988
  • 29
  • 118
  • 161
  • you can still get the long value of your Date object, can't u? – Kent Oct 16 '11 at 13:03
  • @xEnOn: you're not converting a Unix timestamp into a date by using the long constructor. A Unix timestamp, as I commented on jackrabbit's answer, is expressed in seconds, not milliseconds. Here: http://en.wikipedia.org/wiki/Unix_time and here: http://www.unixtimestamp.com/index.php Seconds. Not milliseconds. – TacticalCoder Oct 16 '11 at 15:06
  • possible duplicate of [Getting "unixtime" in Java](http://stackoverflow.com/questions/732034/getting-unixtime-in-java) – Eran Medan Nov 16 '12 at 00:47

6 Answers6

104

getTime() retrieves the milliseconds since Jan 1, 1970 GMT passed to the constructor. It should not be too hard to get the Unix time (same, but in seconds) from that.

jackrabbit
  • 5,525
  • 1
  • 27
  • 38
  • 1
    Thanks. I kept remembering it wrongly as a static function Date.getTime(new Date()) which didn't work and thought what was going on. – Carven Oct 16 '11 at 13:18
  • 50
    @jackrabbit: actually *getTime()* doesn't give back the Unix timestamp but "Unix timestamp * 1000". The agreed defintion about the Unix timestamp is that it gives times since the epoch in second, not milliseconds ; ) – TacticalCoder Oct 16 '11 at 15:03
  • 3
    @TacticalCoder jackrabbit is more accurate: It returns the unix-timestamp in milliseconds. Yes, the unix-timestamp is agreed to be in seconds - but if your answer would be true, (`getTime() = unixTimestamp * 1000`), `getTime()` would always return three Zeros at the end, but in fact can return anything from ending with `000` to `999`, which means it has a higher precision due to milliseconds and is not just "*1000". Meaning: Comparing a real unixTimestamp*1000 with the `getTime()` result would only succeed in ~0.1% of the cases. – dognose Mar 26 '18 at 08:52
  • For the record, the function returns the miliseconds in GMT-0 area (in Greenwich), So you can be safe about converting this to different areas. – Onat Korucu Dec 06 '19 at 12:37
55

To get a timestamp from Date(), you'll need to divide getTime() by 1000, i.e. :

Date currentDate = new Date();
currentDate.getTime() / 1000;
// 1397132691

or simply:

long unixTime = System.currentTimeMillis() / 1000L;
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
26
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Timeconversion {
    private DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH); //Specify your locale

    public long timeConversion(String time) {
        long unixTime = 0;
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+5:30")); //Specify your timezone
        try {
            unixTime = dateFormat.parse(time).getTime();
            unixTime = unixTime / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return unixTime;
    }
}
Kedar Tendolkar
  • 474
  • 3
  • 9
bhasker
  • 645
  • 1
  • 7
  • 20
8

In java 8, it's convenient to use the new date lib and getEpochSecond method to get the timestamp (it's in second)

Instant.now().getEpochSecond();

waterscar
  • 876
  • 1
  • 8
  • 14
  • But the code presented returns the # of seconds for **this** moment – Milan Velebit Apr 06 '19 at 21:45
  • 1
    True, @MilanVelebit. You will have to substitute your desired `Instant` instead of `Instant.now()`. Or `yourDesiredJavaUtilDate.toInstant()` if you got an old-fashioned `Date` from a legacy API. – Ole V.V. Oct 05 '20 at 16:19
0

I dont know if you want to achieve that in js or java, in js the simplest way to get the unix timestampt (this is time in seconds from 1/1/1970) it's as follows:

var myDate = new Date();
console.log(+myDate); // +myDateObject give you the unix from that date
elverde
  • 193
  • 2
  • 7
-2

Use SimpleDateFormat class. Take a look on its javadoc: it explains how to use format switches.

AlexR
  • 114,158
  • 16
  • 130
  • 208