Use Calendar instead for locale sensitive info (timezone, dst, etc). Code snippets:
Date timeStamp = ...; ... // Get the system calendar for 'here' and set the timestamp (universal time). Calendar cal = Calendar.getInstance(); cal.setTime(timeStamp); // Get the system calendar for 'somewhere' and set the timestamp. // Now use 'cal2' to get lo TimeZone somewhereTZ = ...; Locale somewhereLoc = ... ; ... Calendar cal2 = Calendar.getInstance(somewhereTZ, somewhereLoc); cal2.setTime(timeStamp); Now you can use 'cal' and 'cal2' to get proper timezone/dst for your date information. The 'cal' will give you info for 'here' (where the device is configured to be) and 'cal2' will give you info for 'somewhere' else :-). To get proper string-formatting and parsing (user output/input), use the DateFormat df = DateFormat.getInstance() and call df.setCalendar (cal) (or df.setCalendar(cal2)), and then call 'format' or 'parse' on this 'df'. On Jul 20, 10:49 pm, Cheng Zhong <[email protected]> wrote: > Thanks very much. > > BTW, is it better to use Calendar instead of Date in most condition? > Many methods in Date are marked as deprecated. > > On Jul 20, 4:11 am, Brian Conrad <[email protected]> wrote: > > > > > You're lucky because DST is a mess with different starting and ending > > times all over the world (and the countries will often change them > > fromtimetotime). It is generally not used in countries near the equator > > like India where sunrise and sunset won't vary that much during the year > > to make it practical. Here's what I use to get the currenttimezone > > whether the phone is set to a zone using DST or not: > > Calendar c = Calendar.getInstance(); > > double z = (double)c.getTimeZone().getOffset(c.getTimeInMillis()) / > > 3600000.0; > > > This will return a value such as -8.00 if it is Pacific StandardTime > > and not DST and -7.00 ifDaylightSavingTimeis in effect. > > There are other methods such as: > > Calendar cal = Calendar.getInstance(); > > double zone = > > (cal.get(Calendar.ZONE_OFFSET)+cal.get(Calendar.DST_OFFSET))/3600000.0; > > > I believe I found the first one may automatically change with DST and > > the second one may too. > > > The general rule with DST is spring forward (you set the clock ahead one > > hour in the spring) and fall back (you set the clock back one hour to > > standardtimein the fall). When these spring and fall change dates > > occur is the mess. > > > - Brian > > > Cheng Zhong wrote: > > > Hi all, > > > > What will Date.getTime() returns in a region withDaylightSavingTime > > > enabled? > > > > Does this function returns a DSTtimeor not? > > > > I call Date.getTime() in a place with DST (such as U.S.), and convert > > > it to a localtimein China (no DST), I get a wrongtime(one hour > > > ahead of localtimein China). How can I fixed this problem? > > > > Maybe I didn't tell tings clearly, but we don't use DST in China... I > > > don't understand DST very well.- Hide quoted text - > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

