Get GMT Time in Milliseconds using JAVA [duplicate]
Asked Answered
O

4

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.

Obie answered 23/10, 2012 at 9:34 Comment(3)
@Mark I think you have not read the question properly. I have taken the reference of the link that you have given in that I am getting date but i want the date in milliseconds On that I stuck. Do you have any Idea????Obie
I don't know why ppl down voted the question without giving any proper reason.Obie
It does give it in millisecondsRoswell
B
21

Use Calendar#getTimeInMillis:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Belen answered 23/10, 2012 at 9:38 Comment(4)
That is the most complicated and inefficient way of getting the GMT time I have seen. :D Under all this it has to call System.currentTimeMillis() which returns the time in millis for GMT.Wanyen
@PeterLawrey: Why do you assume OP always wants the current time?Facesaving
That is true, but if you need to store the GMT time in millis, you can use long for that too.Wanyen
Will it also handle daylight saving correctly?Sofer
C
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 :)

Coben answered 23/10, 2012 at 9:39 Comment(2)
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?Jurisdiction
This returns the exact same result as the accepted answer, but is much more concise.Thought
B
21

Use Calendar#getTimeInMillis:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Belen answered 23/10, 2012 at 9:38 Comment(4)
That is the most complicated and inefficient way of getting the GMT time I have seen. :D Under all this it has to call System.currentTimeMillis() which returns the time in millis for GMT.Wanyen
@PeterLawrey: Why do you assume OP always wants the current time?Facesaving
That is true, but if you need to store the GMT time in millis, you can use long for that too.Wanyen
Will it also handle daylight saving correctly?Sofer
A
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.

Anana answered 23/10, 2012 at 9:40 Comment(1)
For current time in millis always prefer System.currentTimeMillis() and also if you are using Calendar prefer cal.getTimeInMillis() instead of cal.getTime().getTime()Carmellacarmelle
W
0

Use getTime() function:

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

Weig answered 23/10, 2012 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.