Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #24156
| From | Stanimir Stamenkov <s7an10@netscape.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Possible to treat time in milliseconds as a different time zone? |
| Date | 2013-05-21 23:09 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <kngk18$pha$1@dont-email.me> (permalink) |
| References | <9aa4dbef-0212-4823-9b31-1a6d54ee772c@googlegroups.com> |
Tue, 21 May 2013 07:58:28 -0700 (PDT), /laredotornado@zipmail.com/:
> 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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Possible to treat time in milliseconds as a different time zone? laredotornado@zipmail.com - 2013-05-21 07:58 -0700
Re: Possible to treat time in milliseconds as a different time zone? Arne Vajhøj <arne@vajhoej.dk> - 2013-05-21 11:23 -0400
Re: Possible to treat time in milliseconds as a different time zone? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-21 11:31 -0400
Re: Possible to treat time in milliseconds as a different time zone? Stanimir Stamenkov <s7an10@netscape.net> - 2013-05-21 23:09 +0300
Re: Possible to treat time in milliseconds as a different time zone? Nigel Wade <nmw@ion.le.ac.uk> - 2013-05-22 10:00 +0100
Re: Possible to treat time in milliseconds as a different time zone? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-05-22 13:08 +0300
Re: Possible to treat time in milliseconds as a different time zone? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-05-22 16:46 +0200
Re: Possible to treat time in milliseconds as a different time zone? Roedy Green <see_website@mindprod.com.invalid> - 2013-05-22 03:03 -0700
Re: Possible to treat time in milliseconds as a different time zone? Robert Klemme <shortcutter@googlemail.com> - 2013-05-26 14:23 +0200
Re: Possible to treat time in milliseconds as a different time zone? Michal Kleczek <michal_wytnijto@kleczek.org> - 2013-05-27 11:45 +0200
csiph-web