1 2 3 Previous Next 713 Replies Latest reply: Jul 17, 2010 2:27 PM by 796440 RSS

    how to get current time in GMT ?

    807580
      hey all i want to get the current timestamp in GMT any ideas how to do that ?
        • 1. Re: how to get current time in GMT ?
          796440
          cs.student wrote:
          hey all i want to get the current timestamp in GMT any ideas how to do that ?
          Create a java.text.SimpleDateFormat. Set its TZ to GMT. Get the current time in a java.util.Date object. Pass that Date object to the SimpleDateFormat to get a formatted string.

          Note, however, that TZ applies only to String representations of dates. A Date object has no associated TZ. It's simply a long representing the number of elapsed millis since an epoch.
          • 2. Re: how to get current time in GMT ?
            807580
            i already did that, and got 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
            • 3. Re: how to get current time in GMT ?
              796440
              Timestamp, like Date, does not have a TZ.

              For example:

              1:23 PM in PDT is 4:23 PM in EDT. It's the same instant, in two TZs that are 3 hours apart. If, at 1:23 PM, my Java program is running in Los Angeles (PDT), and it creates a Date or Timestamp object, and at the same instant, which is 4:23 PM in New York (EDT) my friend's Java program creates a Date or Timestamp object, his object will be identical to mine. The only thing that distinguishes a Date or Timestamp object is a long or a long plus int to represent the instant in which it was created.

              If we take our identical, TZ-free Date or Timestamp objects and pass them to DateFormat.format() where one is set to PDT and one to EDT, we'll get 1:23 and 4:23 respectively.

              Finally, if at the same instant--1:23 PM in PDT and 4:23 PM in EDT--I call parse() on the String "1:23" and my friend calls parse() on the String "4:23", we'll get identical Date objects.

              Again: Date and Timestamp do not include TZ.
              • 4. Re: how to get current time in GMT ?
                796440
                cs.student wrote:
                //need to convert the string to equivalent timestamp object
                java.util.Date date = dateFormat.parse(timeString);
                java.sql.Timestamp ts = new ts(date.getTime());
                • 5. Re: how to get current time in GMT ?
                  807580
                  java.sql.Timestamp does not have an explicit timezone. To quote the Javadoc, it is just "a thing wrapper round java.util.Date" It has an implied timezone of UTC (same as GMT) and cannot be changed. I say again - it cannot be changed.
                  • 6. Re: how to get current time in GMT ?
                    796440
                    sabre150 wrote:
                    java.sql.Timestamp does not have an explicit timezone. To quote the Javadoc, it is just "a thing wrapper round java.util.Date"
                    Yup.
                    It has an implied timezone of UTC (same as GMT) and cannot be changed. I say again - it cannot be changed.
                    I disagree. Though the definition is "millis since 1/1/1970 00:00:00.000 UTC", there's nothing in the object to reflect that it's 1/1/70 00:00 or that it's UTC. If the definition were changed tomorrow to "millis since 12/31/1969 16:00:00.000 PST", the class would not change at all. I wouldn't say that the identical class has an "implied TZ" of PST vs. UTC just by that change.

                    So while it's defined relative to a time in UTC, there's no TZ--implied or otherwise--that's part of the object.

                    Yeah, I'm having a hard time focusing on work now, and felt a little pointless ballsniffing pedantry would be refreshing. :-)
                    • 7. Re: how to get current time in GMT ?
                      807580
                      jverd wrote:
                      sabre150 wrote:
                      java.sql.Timestamp does not have an explicit timezone. To quote the Javadoc, it is just "a thing wrapper round java.util.Date"
                      Yup.
                      It has an implied timezone of UTC (same as GMT) and cannot be changed. I say again - it cannot be changed.
                      I disagree. Though the definition is "millis since 1/1/1970 00:00:00.000 UTC", there's nothing in the object to reflect that it's 1/1/70 00:00 or that it's UTC. If the definition were changed tomorrow to "millis since 12/31/1969 16:00:00.000 PST", the class would not change at all. I wouldn't say that the identical class has an "implied TZ" of PST vs. UTC just by that change.

                      So while it's defined relative to a time in UTC, there's no TZ--implied or otherwise--that's part of the object.
                      There is no field in Timestamp or Date that hold a Timezone so neither has an explicit* Timezone. The timezone is implied by the long value it holds being the defined as being the number of milliseconds since 1/1/1970 UTC. Of course this is only by convention. One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.

                      Of course if you were really really bored and really felt like doing something pointless you could re-define the units for the long value and define your own epoch and write your own Date helper classes.

                      >
                      Yeah, I'm having a hard time focusing on work now, and felt a little pointless ballsniffing pedantry would be refreshing. :-)
                      :-) I suggest a couple of strong cups of Typhoo (If someone hasn't already done so, maybe I should create a JTyphoo API). Much better for you than Java.
                      • 8. Re: how to get current time in GMT ?
                        796440
                        sabre150 wrote:
                        There is no field in Timestamp or Date that hold a Timezone so neither has an explicit* Timezone. The timezone is implied by the long value it holds being the defined as being the number of milliseconds since 1/1/1970 UTC. Of course this is only by convention.
                        That it's only by convention is exactly my point. I can also say it's millis since 12/31/69 16:00 PST, and state that therefore the implied TZ is PST. I don't like to even bring up the notion of an "implied" TZ because I think that confuses noobs into thinking that a Date "has" a TZ.
                        One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.
                        Actually, they're interpreting it as millis relative to 12/31/69 16:00 PST.
                        Yeah, I'm having a hard time focusing on work now, and felt a little pointless ballsniffing pedantry would be refreshing. :-)
                        :-) I suggest a couple of strong cups of Typhoo (If someone hasn't already done so, maybe I should create a JTyphoo API). Much better for you than Java.
                        I'd rather have a nice, malty beer.
                        • 9. Re: how to get current time in GMT ?
                          807580
                          jverd wrote:
                          One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.
                          Actually, they're interpreting it as millis relative to 12/31/69 16:00 PST.
                          :-) xenophobe.

                          >
                          Yeah, I'm having a hard time focusing on work now, and felt a little pointless ballsniffing pedantry would be refreshing. :-)
                          :-) I suggest a couple of strong cups of Typhoo (If someone hasn't already done so, maybe I should create a JTyphoo API). Much better for you than Java.
                          I'd rather have a nice, malty beer.
                          I see that you and Jos are of one mind. Come to think of it - I have never seen you and Jos in the same room at the same time.
                          • 10. Re: how to get current time in GMT ?
                            796440
                            sabre150 wrote:
                            I see that you and Jos are of one mind. Come to think of it - I have never seen you and Jos in the same room at the same time.
                            Shhh! Don't tell anybody.
                            • 11. Re: how to get current time in GMT ?
                              796440
                              sabre150 wrote:
                              jverd wrote:
                              One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.
                              Actually, they're interpreting it as millis relative to 12/31/69 16:00 PST.
                              :-) xenophobe.
                              Oops. I meant 1/1/70 09:00 JST
                              • 12. Re: how to get current time in GMT ?
                                Darryl Burke
                                jverd wrote:
                                sabre150 wrote:
                                jverd wrote:
                                One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.
                                Actually, they're interpreting it as millis relative to 12/31/69 16:00 PST.
                                :-) xenophobe.
                                Oops. I meant 1/1/70 09:00 JST
                                :sniff: Everybody knows the epoch started at 01-01-1970 05:30 IST
                                • 13. Re: how to get current time in GMT ?
                                  807580
                                  DarrylBurke wrote:
                                  jverd wrote:
                                  sabre150 wrote:
                                  jverd wrote:
                                  One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.
                                  Actually, they're interpreting it as millis relative to 12/31/69 16:00 PST.
                                  :-) xenophobe.
                                  Oops. I meant 1/1/70 09:00 JST
                                  :sniff: Everybody knows the epoch started at 01-01-1970 05:30 IST
                                  Does that mean I'm 5.5 hours older than you Darryl?
                                  • 14. Re: how to get current time in GMT ?
                                    Darryl Burke
                                    sabre150 wrote:
                                    DarrylBurke wrote:
                                    jverd wrote:
                                    sabre150 wrote:
                                    jverd wrote:
                                    One can insert any long value one wants in it and say that this is the offset since any arbitrary epoch in arbitrary units in an arbitrary timezone but then non of the helper classes such as SimpleDateFormat would function correctly since they interpret the long value as being milliseconds relative to 1/1/1970 UTC.
                                    Actually, they're interpreting it as millis relative to 12/31/69 16:00 PST.
                                    :-) xenophobe.
                                    Oops. I meant 1/1/70 09:00 JST
                                    :sniff: Everybody knows the epoch started at 01-01-1970 05:30 IST
                                    Does that mean I'm 5.5 hours older than you Darryl?
                                    No, you're 5.5 hours younger
                                    1 2 3 Previous Next