0

i build a new website.but the host is in USA.i am not in USA. i need get the time on the website page to compare with one local Variable. But because of time difference,it has 8 hous difference。how to solve this problom?

my code

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
java.util.Date currentTime = new java.util.Date();  
String dateString = formatter.format(currentTime);  `

how to revise these code ?

alex
  • 165
  • 1
  • 1
  • 6
  • 1
    Related: http://stackoverflow.com/questions/2532729/daylight-saving-time-and-timezone-best-practices – BalusC Jan 31 '11 at 14:40

4 Answers4

6

java.util.Date does not support timezones. You should pass the TimeZone to the formatter instead, by calling formatter.setTimeZone(tz).

joda-time is considered a better choice when working with dates. Note that for the sake of formatting it is fine to use Date, but it is a general advise not to rely on it when it comes to i18n. (Note the many deprecated methods there)

Then make each user set his timezone. Ideally suggest / assume the timezone based on his browser locale. See here

And always store the dates in a fixed timezone - preferably GMT/UTC.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    +1 for storing times in GMT, but -1 for saying not to use `Date`. There's nothing wrong with using `Date`, it just defines a point in time. All you need to do is tell the formatter what `TimeZone` you want the dates formatting in. – SimonC Jan 31 '11 at 14:45
  • @SimonC - it is not OK to use Date, because it uses the default timezome of the JVM. And while this is indeed fine for formatting (by using an external formatter), it was a general advise not to use it. – Bozho Jan 31 '11 at 14:48
  • @Bozho why do you say it is not OK? As long as you treat `Date` objects as simply a point in time since the epoch and use the correct timezone when dealing with calendars or formatting then you won't go wrong. I.e. don't call `toString()` or any of the deprecated method like `getHours()`. – SimonC Jan 31 '11 at 15:05
  • @SimonC - well, it is safe with a number of preconditions. And that's why it's tricky. The whole class hasn't been deprecated, so I agree it has its usecases, but might be tricky. Plus, it's mutable. All this has led to replacements like Calendar, `DateTime` and the upcoming standard datetime api. – Bozho Jan 31 '11 at 15:09
  • i put " TimeZone local = TimeZone.getTimeZone("Asia/Tokyo"); Calendar now = Calendar.getInstance(local); SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); String dateString = formatter.format(now.getTime());" – alex Jan 31 '11 at 15:33
  • but, it doesnt work. the time still same with my local time .. not "Asia/Tokyo" time.. why? – alex Jan 31 '11 at 15:35
1

In order to handle timezones, Java includes the Olson timezone database. Find the city in the database that is in the same time zone as you are.

First, you need to get a TimeZone object for the timezone you want. Then, get a Calendar object with the current date and time (or the date and time you wish to use). You can format that with a SimpleDateFormat object.

TimeZone local = TimeZone.getTimeZone("Asia/Tokyo");
Calendar now = Calendar.getInstance(local); // gets time in the current timezone
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(local)

String dateString = formatter.format(now.getTime());

Though if you're doing a lot of time manipulation, like Bozho says, go for joda-time. The Java date/time system is confusing and rather poorly designed.

Paul Fisher
  • 9,618
  • 5
  • 37
  • 53
  • i put u code in my jsp page... but, it doesnt work. the time still same with my local time .. not "Asia/Tokyo" time.. why? – alex Jan 31 '11 at 15:28
  • @alex It's because I neglected to set the timezone on the formatter; I've corrected that. Like I said, it's confusing and poorly-designed ;) – Paul Fisher Jan 31 '11 at 18:02
  • just a little wrong.. " formatter.setTimeZone(local);" thx~ everything is ok now. :P – alex Feb 01 '11 at 07:03
0

In such cases I always change timezone in Linux:

mv /etc/localtime  /etc/localtime-backup
ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime 

It also can be helpful for reading log files for example (I always see my local time instead of calculating it each time when I need to dig into them)

tenshi
  • 26,268
  • 8
  • 76
  • 90
0

I think you need to use a Calendar (they are more useful generally than just Date objects). If you create a Calendar, initialised with your locale and timezone, you can do calendar.setDate() using the date you created. If you create another Calendar object with the fields that were entered, you can then do comparisons between the two Calendar objects.

andrewmu
  • 14,276
  • 4
  • 39
  • 37
  • u say "create a Calendar, initialised with your locale and timezone",can u give me a code example ? – alex Jan 31 '11 at 14:57
  • Sure, if you look at the docs: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#Calendar(java.util.TimeZone, java.util.Locale), you can construct a Calendar with your own timezone/locale. – andrewmu Jan 31 '11 at 15:00
  • Sorry, I should have said, you can get an instance, for example: ` Calendar myCal = Calendar.getInstance(TimeZone.getTimeZone("Europe/London")); ` – andrewmu Jan 31 '11 at 15:10