Date 日期創建問題
每份output文件都要備註上日期時間,冗贅的重複 new Date() 物件又過於沉悶…
不如… 自己寫個轉換日期用的通用格式好了!
交付給同事的檔案,不論是接口拋接、測試coding效率…等等,都需要
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
當日 / 修改日期以下… 為實際產出method時,自己所遇到的小小問題
Solution - 01 . 自己建立一支method處理日期標註問題
由於 coding 當中,有過多的new Date() / new SimpleDate("format") 問題
不如自己獨立一支簡易的method, 用來專門處理產出文件類型的相關日期 method 吧。
需求表 :
input data
- 日期可以由使用者 (User) 自行定義
- 日期格式也由使用者 (User) 自行定義
利用 new Date() 特性,將日期以 String 格式插入 new Date()當中
這時候問題又來了,當要產生執行當下時間的日期該如何是好…
Solution - 02 . 對後端使用 method 友善使用
其實 new Date() 物件只要()內部 不給予任何String、Long或者是int …等等的資料型態
就可取得或當前時間,並且在用 simpleDate 轉換成自己所需的格式即可
那要怎麼做到對其他人使用我的method時,會是較為友善做法呢?
需求表 :
input data
- 原先的method 不做任何的改動
- 依就需要輸入日期格式由使用者 (User) 自行定義
我們再次創建新的 method ,並且不對原本的 convertDateFormat 這支的 targetDate
輸入null / 空字串進行判斷 if/else 處理並且創建 new Date() 不插入值的方法
這對於後端使用者 / 工程師來說,是較為彈性 / 且友善的作法,理由是因為
使用者不必記得 convertDateFormat 的 input 可不可以放 null / 空字串
產出是 …
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
NowTime
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
ModifiedTime
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Solution - 03 . Date.toString v.s. Date.toGMTString
依照 convertNowDateFormat 的想法是把今日轉換成字串,再拋給 convertDateFormat 做使用
理應當在 convertDateFormat 轉出會是正常的,但是經過幾次後呼叫現在時間都會跑歪掉
後來做了點小測試,測試 toString() / toGMTString() 兩種方法呼叫Date
使用 toGMTString() 插入 new Date() 中做轉換可以得到當前日期
反之使用 toString() 插入 new Date() 會產生出比當前時間還要快的日期
做到這其實就已經解決 coding 上的問題了,畢竟文件上需要的是現在的時間
Solution - 04 . Date(String)轉換的問題
解決完問題後,雖然不會再有時間錯亂的格式,但總想知道為何生此問題
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
返回找尋關於 java.util.Date 的 DOCUMENT 中
現在已經被註記掉了,改由 Date.parse(String) ,因此在往上查
這邊 Date.parse(String) 文件過長,有興趣上來這看
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Date.parse(Stirng) - DOCUMENT
擷取主要的幾點問題
- Any word that matches GMT, UT, or UTC, ignoring case, is treated as referring to UTC.
- Any word that matches EST, CST, MST, or PST, ignoring case, is recognized as referring to the time zone in North America that is five, six, seven, or eight hours west of Greenwich, respectively. Any word that matches EDT, CDT, MDT, or PDT, ignoring case, is recognized as referring to the same time zone, respectively, during daylight saving time.
有看出什麼問題點嗎? 沒關係,放上new Date().toString()/toGMTString() 轉換後字串比對看看
從 Date.parse(String) 的文件中可以知道
- 第一點:任意字眼符合 GMT, UT, UTC 皆被共同視為 UTC 時間對待
- 第二點:任意字眼符合 EST, CST, MST, PST 皆被共同視為 North America的時區對待。任意字眼符合 EDT, CDT, MDT, PDT 皆被共同視同時區對待。
new Date() 所產生出的時間為 當地時區時間 + CST + 當前元年
UTC 為世界協調時間(Coordinated Universal Time),當 GMT 被配對到時時間則以 UTC 對待
香港、澳門、臺灣、蒙古國、菲律賓、新加坡、馬來西亞、澳洲西部等等都是 GMT+8 時區
其中 UTC 也可以代表 GMT時間 , 所以當 Date.toGMTString() 時並不會被報錯
因為
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2 Dec 2021 09:16:58 GMT (GMT+8 為
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
地區時間)2021:12:02
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
09:16:58 + 08:00:00 = 17:16:58 :O:
CST 為北美某一時區的時間,大略以美國時區來算為 GMT-6 的地方
而當使用 Thu Dec 02 17:16:58 CST 2021 字串當作 new Date() 插入時間字串
則程式會判讀為 在CST這個地區為 17:16:58 2021:12:02 這一天,這代表甚麼
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
問題很嚴重
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
CST 為 GMT-6 , 台灣為 GMT+8 , 兩者相差了8+6=14小時
當 CST 是 17點時,這代表你現在的時區比 DST 快14小時
2021:12:02
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
17:16:58 + 14:00:00 = 31:16:58 ( 2021:12:03 07:16:58 ) :X:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
( 看到這可以跳為Solution - 03 算算看是不是一樣
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
連結