tags:

views:

94

answers:

3

Possible Duplicate:
How can I get the current date and time in UTC or GMT in Java?

I want to get the current timestamp in GMT; any ideas how to do that?

+2  A: 
new Date().getTime();

Or just

System.currentTimeMillis();

Both assume you take "timestamp" to mean "milliseconds from the Unix epoch". Otherwise, clarify your question.

Edit: In response to the comment/clarification/"answer":

You're misunderstanding the difference between storing a GMT timestamp and displaying it as such. Date/Timestamp will store as milliseconds from the UTC epoch internally, and is not related to a certain timezone, since timestamps are the same regardless of your timezone. If it's 10pm GMT in Honolulu, it's also 10pm GMT in New York. They have the same timestamp, though your location might make them render differently.

A Calendar, on the other hand, is meant for displaying certain fields properly (as in, it's 6pm on the 8th of June) and so does have an internal notion of TimeZone (since 6pm EDT is NOT the same time as 6pm PDT).

Anyway, your example gave a SimpleDateFormat. If you want to display your already-GMT Timestamp in GMT, do the same thing and pass in the Timestamp.

    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SS");
    fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(fmt.format(timestamp));
Mark Peters
i want to get the current time in GMT in a timestamp formati tried the following:Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));Timestamp ts=new Timestamp(cal.getTimeInMillis());but it always gets time in my local
@ylazez: What do you mean by "gets time"? You mean when you print it? You mean when you persist it via a PreparedStatement? Update your question to reflect what you're actually trying to do: give a small piece of code that demonstrates the problem. The way you're doing it uses GMT time internally, though it might print local time depending on what you try to do with it.
Mark Peters
i want to insert timestamp in the database in gmt time, got me ?
@ylazez: Then just put it in a PreparedStatement and go. It's already in GMT, and if it changes in your database then something's wrong with your database driver. Are you constructing the SQL statements as Strings manually or something?
Mark Peters
look i get the current time using:Timestamp timestamp = new Timestamp(System.currentTimeMillis());it always get the time in my local which is GMT+3and i insert this timestamp using hibernate.save to save an object in postgresql databse, the column is timestamp with timezone
@ylazez: Then perhaps you should have been asking that from the start. Take time to give us the information upfront that we need to help you. None of the things you just said are in the question: doesn't that stand out as a problem to you? I would suggest starting from scratch, searching the internet and StackOverflow, and then post a new question with the correct information if you're still stuck.
Mark Peters
+1  A: 
new Date()

...since all Date objects are GMT based.

dty
A: 

i managed to get a string in gmt format the problem is that i want to convert this string to equivalent timestamp object i.e same time but as a timestamp object

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date=new Date();
        String s=sdf.format(date);
        System.out.println("GMT: "+s);
        //need to convert the string to equivalent timestamp object
A java.sql.Timestamp *is-a* java.util.Date so just pass it in instead of `date` above.
Mark Peters