遊客:  註冊 | 登錄 | 幫助





標題: [分享] Java TimeZone
1022292057     Rank: 4Rank: 4
水中藍
性別 男
UID 17104

精華 0
帖子 165
積分 798   詳情

閱讀權限 50
註冊 2006-12-9
來自 Canada
狀態 離線

 
 
 
 
發表於 2008-4-16 11:53 AM  資料  個人空間  主頁 短訊  加為好友 
回復 #10 mickeyGoUp 的帖子

okay, i found the reason after going through the source code for TimeZone class
http://gcc.gnu.org/ml/java-prs/2002-q3/msg00052.html

getDSTSavings() actually doesn't check if daylight saving is in effect or not, it basically just output 1hr if TimeZone HAS a daylight saving system and 0 if TimeZone don't even use daylight saving,
so for EST, getDSTSavings() will always generate 3600000 no matter if daylight saving is in effect or not
got mislead by the wordings in the TimeZone class reference and the way u use it, seems like a lot of ppl misunderstood too as they have issued a bug to java concerning this

so i guess i will rewrite the program, and this should work

private void setCurrentGmtDatetime(String LocationID) {

     TimeZone local = TimeZone.getDefault();
     TimeZone result = TimeZone.getTimeZone(locationID);
     Date currentDate = new Date();
     
     //initial offset resultGMT - localGMT
     int offset = (result.getRawOffset() - local.getRawOffset());

     // if local is in daylight saving effect, adjust offset
     if (local.inDaylightTime(currentDate)) {
          offset -= local.getDSTSavings();
     }

     // do time convertion
     // do result's daylight saving adjustment later as u will need the
     // time in result location to determine if daylight is in effect
     Date newDatetime = new Date(currentDate.getTime() + offset);
     
     // check if daylight saving adjustment applies
     if (result.inDaylightTime(newDatetime)) {
          // use setTime to readjust, call new Date again will allocate extra memory
          newDatetime.setTime(newDatetime.getTime() + result.getDSTSavings());
     }
}

tested - should work

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2008-4-16 07:27 PM  資料  個人空間  短訊  加為好友 
回復 #11 1022292057 的帖子

    
You are really a hardcore programmer, numbie!  Thanks for spending time to figure that out!!  That was what I originally wanted to point out - the inDaylightTime(Date) method.  And yeah, one has to actually call inDaylightTime() to determine whether to apply the DST offset.

Then, the next question I was gonna ask was:  if inDaylightTime() takes the current local datetime as an argument to figure out whether it is in the daylight saving schedule, can I just pass in the current remote datetime for it to figure out whether the remote location is in DST schedule?  Then I saw these in your codes:

QUOTE:
     // do time convertion
     // do result's daylight saving adjustment later as u will need the
     // time in result location to determine if daylight is in effect
     Date newDatetime = new Date(currentDate.getTime() + offset);

     // check if daylight saving adjustment applies
     if (result.inDaylightTime(newDatetime)) {
          // use setTime to readjust, call new Date again will allocate extra memory
          newDatetime.setTime(newDatetime.getTime() + result.getDSTSavings());
     }

So, we are assuming that can be done.  Then, my next series of questions:

How does the inDaylightTime() method know anything about DST from a Date instance, if Date is just a representation of a specific time instant?  

Or Date has more information than just a specific time instant?  

Even if Date has the location info, doing a "currentDate.getTime() + offset" will only adjust the time to a new time, and "new Date(newTimeInMillisecond)" will only instantiate a Date instance with a different time but the same location info(?).  How does it know anything about the remote location?

I guess I don't know enough about Date/Calendar/TimeZone...etc., that's why I like to talk to you guys, and we can learn together.  小神 and lhy are more than welcome to join the discussion!  



最後編輯: mickeyGoUp : 2008-4-16 07:28 PM
頂部

1022292057     Rank: 4Rank: 4
水中藍
性別 男
UID 17104

精華 0
帖子 165
積分 798   詳情

閱讀權限 50
註冊 2006-12-9
來自 Canada
狀態 離線

 
 
 
 
發表於 2008-4-17 02:37 AM  資料  個人空間  主頁 短訊  加為好友 
nah, not really a hardcore programmer, i won't even touch programming stuff unless i have to, u on the other hand always shows interesting stuff and shows interest in learning new stuff, u ar more hardcore
i basically come to this site to download drama , then might as well answer ppl's question
didn't really spent lots of time too,
just the way how i am educated in university, i no my program works with the given functions, but it didn't, then it must be the given functions, then it's natural to just scan through the TimeZone class source, then the reason is clear, only took few mins

back to ur questions...

every TimeZone has a daylight saving schedule either populated with some formula or hardcoded, so it stores information on the range of date and exact time that daylight is in effect or not
when passing in a date to TimeZone.inDaylightTime(Date date), it will then just compare the date with the daylight saving schedule in the TimeZone instance and see if daylight saving is in effect
location info is stored in TimeZone and not Date

Calendar basically same thing as Date with more functionalities, most Date functions have already been deprecated and replaced with corresponding Calendar functions

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2008-4-17 05:20 AM  資料  個人空間  短訊  加為好友 
回復 #13 1022292057 的帖子

Even if you are not hardcore, I can see the spirit in you!  Some people might not even have time to read my lengthy post.  

And I agreed with what you said about how you were trained in University.  Alot of people think University is useless and the knowledge from the books are just not practical enough, but I always think what we learned from college (especially in our field) are the problem solving skills and time management.  From then on, there are tons of resources out there, it is just a matter of how to make use to them.  

Now back to my questions:

Please bear with me for being slow.  Say, if you are the TimeZone class with the inDaylightTime() method.  And I hold the time instant.  Now, I tell you "April 17, 2008 12:30am".  How do you know if I am talking about "April 17, 2008 12:30am" here in the east coast, or "April 17, 2008 12:30am" in Hong Kong?  You know what I mean?

頂部

  小神     Rank: 4Rank: 4
水中藍
性別 保密
UID 11831

精華 0
帖子 2634
積分 1386   詳情

閱讀權限 50
註冊 2006-9-12
來自 天堂
狀態 離線

 
 
 
 
發表於 2008-4-17 07:38 AM  資料  個人空間  短訊  加為好友 
mickey, 個timezone instant 咪有hold 住個location lor..

TimeZone result = TimeZone.getTimeZone(locationID);

呢度咪set 左你個location lor..

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2008-4-17 08:45 AM  資料  個人空間  短訊  加為好友 
回復 #15 小神 的帖子

係喎!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  

我諗我真係太 "支力" 喇!!!  咁都睇漏!!!!  

所以話姐,唔同您地傾下,自己可能仲響都兜圈!!!!!

Numbie,慳返您一個 post!!!  

頂部

  小神     Rank: 4Rank: 4
水中藍
性別 保密
UID 11831

精華 0
帖子 2634
積分 1386   詳情

閱讀權限 50
註冊 2006-9-12
來自 天堂
狀態 離線

 
 
 
 
發表於 2008-4-17 10:10 AM  資料  個人空間  短訊  加為好友 
我有時都係咁..
明明好簡單..
但係就係睇唔到..
搞完一大餐之後先發現...

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2008-4-17 08:31 PM  資料  個人空間  短訊  加為好友 
回復 #17 小神 的帖子

係啦!  我就覺得係 "鬼暗眼"!!!  可唔可以用 Java 寫度符俾我闢一闢邪呢?  

講返正經,真係架喎!  有時對得 d codes 多,有 d 細媚細眼野係會睇唔到,所以當我花左一段時間都搵唔出原因時,我就會叫同時坐響隔黎,我講一次佢知我個問題,解釋下我做緊既野,自自然然就會我地是但一個見到點解會唔 work!  有時個同事只係左響度聽我自問自答,有時就係個同事指出邊度有問題,仲知萬試萬靈!!  

頂部

  小神     Rank: 4Rank: 4
水中藍
性別 保密
UID 11831

精華 0
帖子 2634
積分 1386   詳情

閱讀權限 50
註冊 2006-9-12
來自 天堂
狀態 離線

 
 
 
 
發表於 2008-4-18 07:48 AM  資料  個人空間  短訊  加為好友 
都好wo..
爪哇符..
呢個program 實好值錢..
你平時睇d鬼片..
好多鬼都用電腦同人溝通..
有左呢個program..
少d 撞邪

MICKEY, 快d開始寫la..

mickeyGoUp :
七月節前要寫好添!!! :P

頂部


快速美言











一群熱心會員對本主題作出以下的回覆:

mickeyGoUp: RMK added for ah_cho - PID 167945 [ view ]
mickeyGoUp: RMK added for 小神 - PID 168052 [ view ]
小神: 很好的貼子!! 無言感激!!!
mickeyGoUp: RMK added for 小神 - PID 168388 [ view ]



最後回覆日期: 2008-4-18 11:28 AM
  編輯帖子
快速美言
           


當前時區 GMT+8, 現在時間是 2024-11-23 07:16 AM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.782080 second(s), 9 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver