Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: Possible to treat time in milliseconds as a different time zone? Date: Wed, 22 May 2013 10:00:01 +0100 Lines: 33 Message-ID: References: <9aa4dbef-0212-4823-9b31-1a6d54ee772c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net N2irAHXAXw5Xeh97NA5Qjw7DYNPwiFzF11tmLPAgIOhOCzSTyo Cancel-Lock: sha1:80WXtYCX2ralOsvnjtMZWeId+Sw= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 In-Reply-To: <9aa4dbef-0212-4823-9b31-1a6d54ee772c@googlegroups.com> Xref: csiph.com comp.lang.java.programmer:24163 On 21/05/13 15:58, laredotornado@zipmail.com 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