10

Possible Duplicate:
Get GMT Time in Java

I have taken the reference of the below 2 link :

link1 link2

But I want the GMT date in milliseconds.

Thanks In Advance.

Community
  • 1
  • 1
mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51

4 Answers4

27

You can use System.currentTimeMillis() it returns "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC."

long time = System.currentTimeMillis();

Will do it :)

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
  • 4
    Doesn't that use the system time rather than just using GMT? If so, wouldn't this cause problems if you need the milliseconds to be standardized across more than one time zone? – Alan Nelson Jul 17 '18 at 19:31
  • This returns the exact same result as the accepted answer, but is much more concise. – Lambart Jul 02 '19 at 21:55
21

Use Calendar#getTimeInMillis:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
João Silva
  • 89,303
  • 29
  • 152
  • 158
0

Use the Calendar Class to create an instance with GMT as the timezone.

From that you can get the time in milliseconds.

see below code sample.

public class TimeTest {

    public static void main(String [] args) {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        System.out.println(cal.currentTimeMillis());

    }
}

Hope this helps.

Subba
  • 396
  • 2
  • 13
  • For current time in millis always prefer `System.currentTimeMillis()` and also if you are using `Calendar` prefer `cal.getTimeInMillis()` instead of `cal.getTime().getTime()` – Sujay Jun 21 '13 at 09:50
0

Use getTime() function:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130