Possible to treat time in milliseconds as a different time zone?

Discussion in 'Java' started by laredotornado@zipmail.com, May 21, 2013.

  1. Guest

    Hi,

    I'm using Java 6. I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT. I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...

    long timeInMs = 1368921600000;
    final Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    cal.setTimeInMillis(timeInMs);
    final java.util.Date dateObj = cal.getTime();
    System.out.println(dateObj.toString());

    Can I parse the time zone string to get the number of hours difference and then just add that? Grateful for any elegant solutions. Thanks, - Dave
    , May 21, 2013
    #1
    1. Advertising

  2. Arne Vajhøj Guest

    On 5/21/2013 10:58 AM, wrote:
    > I'm using Java 6. I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT. I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...
    >
    > long timeInMs = 1368921600000;
    > final Calendar cal = Calendar.getInstance();
    > cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    > cal.setTimeInMillis(timeInMs);
    > final java.util.Date dateObj = cal.getTime();
    > System.out.println(dateObj.toString());
    >
    > Can I parse the time zone string to get the number of hours difference and then just add that? Grateful for any elegant solutions.


    The idea sounds wrong to me.

    In Java time stored in a long is supposed to be in UTC. And there are
    various classes to convert to and from time components based on time
    zones.

    Inventing a ms since 1970 local time concept would create massive
    confusion in the code base.

    Arne
    Arne Vajhøj, May 21, 2013
    #2
    1. Advertising

  3. Eric Sosman Guest

    On 5/21/2013 10:58 AM, wrote:
    > Hi,
    >
    > I'm using Java 6. I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT. I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...
    >
    > long timeInMs = 1368921600000;
    > final Calendar cal = Calendar.getInstance();
    > cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    > cal.setTimeInMillis(timeInMs);
    > final java.util.Date dateObj = cal.getTime();
    > System.out.println(dateObj.toString());


    What do you mean by "doesn't work?" (Besides "doesn't compile,"
    that is.) What were you hoping to get, and what did you see, and
    in what way did it disappoint you?

    > Can I parse the time zone string to get the number of hours difference and then just add that? Grateful for any elegant solutions. Thanks, - Dave


    The getTimeZone() method has already parsed the string (or
    tried to), and you can extract its results from the TimeZone object.

    --
    Eric Sosman
    d
    Eric Sosman, May 21, 2013
    #3
  4. Tue, 21 May 2013 07:58:28 -0700 (PDT), //:

    > I'm using Java 6. I'm trying to see if there's a simple way to
    > convert a long varaible (the number of milliseconds since 1970)
    > to a timezone other than GMT. I have another time zone string,
    > MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm
    > figuring out this doesn't work ...
    >
    > long timeInMs = 1368921600000;
    > final Calendar cal = Calendar.getInstance();
    > cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    > cal.setTimeInMillis(timeInMs);
    > final java.util.Date dateObj = cal.getTime();
    > System.out.println(dateObj.toString());
    >
    > Can I parse the time zone string to get the number of hours
    > difference and then just add that? Grateful for any elegant
    > solutions. Thanks, - Dave


    What do you expect when you say "convert"?

    As others have pointed out the java.util.Date type is a simple
    wrapper for a timestamp value which is always in UTC. If you
    initialize a java.util.Calendar instance and set its timeZone -
    you've already converted it in a way. When you query the Calendar
    fields HOUR_OF_DAY, MINUTE, etc. you've got your conversion. You
    may use these values to initialize your custom type of time object,
    also.

    If you want to _format_ a time/timestamp in a text representation
    using specific time-zone, instead, you may use a java.util.DateFormat:

    final String MY_TIMEZONE = "JST";
    long timeInMs = 1368921600000L;
    DateFormat dateFormat = DateFormat.getDateTimeInstance();
    System.out.println(dateFormat.format(new Date(timeInMs)));
    dateFormat.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    System.out.println(dateFormat.format(new Date(timeInMs)));

    Note, your example code does really nothing to the Calendar.time
    value, even if you change the Calendar's timeZone in between:

    long timeInMs = 1368921600000L;
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    cal.setTimeInMillis(timeInMs);
    java.util.Date dateObj = cal.getTime();
    cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE2));
    java.util.Date dateObj2 = cal.getTime();
    System.out.println(dateObj.getTime() == dateObj2.getTime());

    --
    Stanimir
    Stanimir Stamenkov, May 21, 2013
    #4
  5. Nigel Wade Guest

    On 21/05/13 15:58, wrote:
    > Hi,
    >
    > I'm using Java 6. I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT. I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...
    >
    > long timeInMs = 1368921600000;
    > final Calendar cal = Calendar.getInstance();
    > cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    > cal.setTimeInMillis(timeInMs);
    > final java.util.Date dateObj = cal.getTime();
    > System.out.println(dateObj.toString());
    >
    > Can I parse the time zone string to get the number of hours difference and then just add that? Grateful for any elegant solutions. Thanks, - Dave
    >


    Timezone is an artificial concept.

    In Java the Date object represents a particular instant in time, and is the number of milliseconds since 1970. This is
    independent of timezone. You can't "convert" it to another timezone. This principle underpins the entire Date, Calendar
    and TimeZone concept.

    In order to view a Date in "human" terms, i.e. wallclock time, timezones are used. This converts an instant in time
    represented by the Date object into a wallclock time in a particular timezone.

    For example, the time in UK now is 09:50 BST. This represents a particular instant in time and has an associated Date
    value. In New York the wallclock time is 04:50 EDT. However, the Date for the UK time is the same as the Date for the
    New York time since they are the same instant in time, only the local representation changes.

    So, your code won't work since all timezones will have the same Date for a particular instant in time. Your objective
    appears to be backwards. What are you actually trying to achieve?

    --
    Nigel Wade
    Nigel Wade, May 22, 2013
    #5
  6. Roedy Green Guest

    On Tue, 21 May 2013 07:58:28 -0700 (PDT),
    wrote, quoted or indirectly quoted someone who said :

    >I'm using Java 6. I'm trying to see if there's a simple way to convert a l=
    >ong varaible (the number of milliseconds since 1970) to a timezone other th=
    >an GMT. I have another time zone string, MY_TIMEZONE, which could be a tim=
    >ezone string ("GMT-5"), but I'm figuring out this doesn't work ...


    see http://mindprod.com/jgloss/calendar.html
    for various conversion recipes.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Technological possibilities are irresistible to man.
    If man can go to the moon, he will.
    If he can control the climate, he will.
    ~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)
    Roedy Green, May 22, 2013
    #6
  7. Nigel Wade <> writes:

    > Timezone is an artificial concept.
    > In Java the Date object represents a particular instant in time, and is
    > the number of milliseconds since 1970. This is


    "1970" is as artificial as timezone.
    The beginning of the year is just as timezone-dependent as any other
    point of time after that.
    The Date object represents milliseconds after beginning of year 1970 in
    a specific timezone.

    --
    Jukka Lahtinen
    Jukka Lahtinen, May 22, 2013
    #7
  8. On 22/05/2013 12:08, Jukka Lahtinen allegedly wrote:
    > Nigel Wade <> writes:
    >
    >> Timezone is an artificial concept.
    >> In Java the Date object represents a particular instant in time, and is
    >> the number of milliseconds since 1970. This is

    >
    > "1970" is as artificial as timezone.
    > The beginning of the year is just as timezone-dependent as any other
    > point of time after that.


    Nigel was being concise since he assumed he was talking to an informed
    public. The complete formulation would have been: "the number of
    milliseconds since midnight, January 1st, 1970 *UTC*". So it does indeed
    represent a specific instant in time.

    > The Date object represents milliseconds after beginning of year 1970 in
    > a specific timezone.


    No. A Date object can represent any date as understood by humans. For
    its *internal representation*, it uses the number of milliseconds since
    midnight, January 1st, 1970 UTC, which it then converts to a calendar
    day and timezone-specific time -- according to the settings of your
    Calendar object and things like leap seconds, etc. -- for representation
    purposes.

    But to the API, the number of milliseconds always means the same thing.
    You can change its meaning, but the API won't understand it.

    --
    DF.
    Daniele Futtorovic, May 22, 2013
    #8
  9. On 21.05.2013 16:58, wrote:

    > I'm using Java 6. I'm trying to see if there's a simple way to
    > convert a long varaible (the number of milliseconds since 1970) to a
    > timezone other than GMT.


    As others have said already that is not necessary. You need to
    distinguish two concepts:

    1. point in time: this is represented by a number of milliseconds since
    a particular point in time in history.

    2. representation of a point in time for various audiences (language,
    time zone).

    Ideally internally to an application you use concept number 1 _all_ the
    time and make use of concept 2 when appropriate (e.g. when presenting a
    point in time to the user or writing it into a logfile).

    > Can I parse the time zone string to get the number of hours
    > difference and then just add that? Grateful for any elegant
    > solutions.


    You can - but it's the wrong approach.

    Cheers

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, May 26, 2013
    #9
  10. On 2013-05-21 16:58, wrote:
    >
    > Can I parse the time zone string to get the number of hours difference and then just add that?


    That's tricky since time difference between timezones is actually time
    dependent because of DST and other requlatory changes.

    --
    Michal
    Michal Kleczek, May 27, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VmluY2UgVmFyYWxsbw==?=

    prevent a postback when moving web parts from zone to zone.

    =?Utf-8?B?VmluY2UgVmFyYWxsbw==?=, Feb 8, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    573
    =?Utf-8?B?VmluY2UgVmFyYWxsbw==?=
    Feb 8, 2006
  2. Replies:
    2
    Views:
    389
  3. mark4asp
    Replies:
    1
    Views:
    287
    Eliyahu Goldin
    Mar 5, 2007
  4. Replies:
    3
    Views:
    1,971
    Steven D'Aprano
    Jul 25, 2009
  5. Geoff Robinson
    Replies:
    0
    Views:
    99
    Geoff Robinson
    Jul 31, 2004
Loading...

Share This Page