---
title: Date 日期創建問題
tags: Java, java.util.Date, toString(), toGMTString()
---
Date 日期創建問題
===
###### tags: `Java` `java.util.Date` `toString()` `toGMTString()`
> 每份output文件都要備註上日期時間,冗贅的重複 new Date() 物件又過於沉悶...
>
> 不如.... 自己寫個轉換日期用的通用格式好了!
交付給同事的檔案,不論是接口拋接、測試coding效率...等等,都需要 :arrow_right: 當日 / 修改日期
以下... 為實際產出method時,自己所遇到的小小問題
---
### Solution - 01 . 自己建立一支method處理日期標註問題
由於 coding 當中,有過多的new Date() / new SimpleDate("format") 問題
不如自己獨立一支簡易的method, 用來專門處理產出文件類型的相關日期 method 吧。
<font size=5>需求表 : </font>
<center><font size=5>input data</font></center>
- 日期可以由使用者 (User) 自行定義
- 日期格式也由使用者 (User) 自行定義
```java
private String convertDateFormat(String targetDate, String formatDate) {
SimpleDateFormat sdf = new SimpleDateFormat(formatDate);
Date date = new Date(targetDate);
return sdf.format(date);
}
```
利用 **new Date() 特性**,將日期以 String 格式插入 new Date()當中
這時候問題又來了,當要產生執行當下時間的日期該如何是好....
---
### Solution - 02 . 對後端使用 method 友善使用
其實 new Date() 物件只要()內部 不給予任何String、Long或者是int ...等等的資料型態
就可取得或當前時間,並且在用 simpleDate 轉換成自己所需的格式即可
那要怎麼做到對其他人使用我的method時,會是較為友善做法呢?
<font size=5>需求表 : </font>
<center><font size=5>input data</font></center>
- **原先的method** 不做任何的改動
- 依就需要輸入日期格式由使用者 (User) 自行定義
```java
private String convertNowDateFormat(String formatDate) {
return convertDateFormat(new Date().toString(), formatDate);
}
```
我們再次創建新的 method ,並且不對原本的 convertDateFormat 這支的 targetDate
輸入null / 空字串進行判斷 if/else 處理並且創建 new Date() 不插入值的方法
這對於後端使用者 / 工程師來說,是**較為彈性 / 且友善的作法**,理由是因為
使用者不必記得 convertDateFormat 的 <font color=red>**input 可不可以放 null / 空字串**</font>
產出是 ... :arrow_right: **NowTime** :question: **ModifiedTime** :question:
---
### Solution - 03 . Date.toString v.s. Date.toGMTString
依照 convertNowDateFormat 的想法是把今日轉換成字串,再拋給 convertDateFormat 做使用
理應當在 convertDateFormat 轉出會是正常的,但是經過幾次後呼叫現在時間都會跑歪掉
後來做了點小測試,測試 toString() / toGMTString() 兩種方法呼叫Date
```java
public void DateNow() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
Date dateO = new Date();
System.out.println("Date using toString()\n");
System.out.println("----- Now Time -------\n");
System.out.println("Now Time is : " + sdf.format(dateO) + '\n');
Date dateOToString = new Date(dateO.toString());
System.out.println("ADDing new Date().toString() in new Date()");
System.out.println(" ToString : " + sdf.format(dateOToString) + '\n');
Date dateOToGTMString = new Date(dateO.toGMTString());
System.out.println("ADDing new Date().toGMTString() in new Date()");
System.out.println("ToGMTString : " + sdf.format(dateOToGTMString));
}
```
```
Date using toString()
----- Now Time -------
Now Time is : 2021:12:02 17:01:06
ADDing new Date().toString() in new Date()
ToString : 2021:12:03 07:01:06
ADDing new Date().toGMTString() in new Date()
ToGMTString : 2021:12:02 17:01:06
```
使用 toGMTString() 插入 new Date() 中做轉換可以得到當前日期
反之使用 toString() 插入 new Date() 會產生出比當前時間還要快的日期
<font color=red>**做到這其實就已經解決 coding 上的問題了,畢竟文件上需要的是現在的時間**</font>
---
### Solution - 04 . Date(String)轉換的問題
解決完問題後,雖然不會再有時間錯亂的格式,但總想知道為何生此問題 :question:
返回找尋關於 java.util.Date 的 DOCUMENT 中
```
@Deprecated
public Date(String s)
Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
Allocates a Date object and initializes it so that it represents the date and time indicated by the string s, which is interpreted as if by the parse(java.lang.String) method.
```
現在已經被註記掉了,改由 Date.parse(String) ,因此在往上查
這邊 Date.parse(String) 文件過長,有興趣上來這看 :arrow_right: [Date.parse(Stirng) - DOCUMENT](https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#parse(java.lang.String))
**<center>擷取主要的幾點問題</center>**
- 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() 轉換後字串比對看看
```java
Date dateO = new Date();
System.out.println(" Now Time : " + dateO;
System.out.println(" ToString : " + dateO.toString());
System.out.println("ToGMTString : " + dateO.toGMTString());
```
```
Now Time : Thu Dec 02 17:16:58 CST 2021
ToString : Thu Dec 02 17:16:58 CST 2021
ToGMTString : 2 Dec 2021 09:16:58 GMT
```
從 Date.parse(String) 的文件中可以知道
- 第一點:任意字眼符合 GMT, UT, UTC 皆被共同視為 UTC 時間對待
- 第二點:任意字眼符合 EST, CST, MST, PST 皆被共同視為 North America的時區對待。任意字眼符合 EDT, CDT, MDT, PDT 皆被共同視同時區對待。
new Date() 所產生出的時間為 **<font color=red>當地時區時間 + CST + 當前元年</font>**
---
UTC 為世界協調時間(Coordinated Universal Time),當 GMT 被配對到時時間則以 UTC 對待
香港、澳門、臺灣、蒙古國、菲律賓、新加坡、馬來西亞、澳洲西部等等都是 GMT+8 時區
其中 UTC 也可以代表 GMT時間 , 所以當 Date.toGMTString() 時並不會被報錯
因為 :arrow_right: 2 Dec 2021 09:16:58 GMT (GMT+8 為:tm:地區時間)
2021:12:02 :arrow_right: 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** 這一天,這代表甚麼 :question:
**問題很嚴重 :heavy_exclamation_mark:** CST 為 GMT-6 , 台灣為 GMT+8 , 兩者相差了8+6=**14小時**
當 CST 是 17點時,這代表你現在的時區比 <font color=red>**DST 快14小時**</font>
2021:12:02 :arrow_right: 17:16:58 **+** 14:00:00 **=** 31:16:58 ( 2021:12:03 07:16:58 ) :X:
:arrow_right: ( 看到這可以跳為Solution - 03 算算看是不是一樣 :rolling_on_the_floor_laughing: :rolling_on_the_floor_laughing: :rolling_on_the_floor_laughing: )
---
### :computer: 連結
<div class="link-Table">
| 參考網站 | 連結 |
|:----------------------------- |:--------------------------------------- |
| Date.parse(Stirng) - DOCUMENT | [:link:][Date.parse(Stirng) - DOCUMENT] |
[Date.parse(Stirng) - DOCUMENT]:(https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#parse(java.lang.String))
</div>
<style>
div.foo > table th:nth-of-type(1) {
width: 50vw;
}
div.foo > table th:nth-of-type(2) {
width: 50vw;
}
div.link-Table > table th:nth-of-type(1) {
width: 80vw;
}
div.link-Table > table th:nth-of-type(2) {
width: 10vw;
}
</style>