Java數(shù)據(jù)類型中Date數(shù)據(jù)類型在處理一些日常應(yīng)用問(wèn)題常常被人們使用。
然而在使用過(guò)程中許多人卻沒有真正的去了解有關(guān)于Java 數(shù)據(jù)類型中Date數(shù)據(jù)類型.
通常在這使用某一種數(shù)據(jù)類型過(guò)程中并不一定就會(huì)有哦相應(yīng)的數(shù)據(jù)類型,往往存在各種各樣的數(shù)據(jù)類型不斷的更替轉(zhuǎn)換。
1.直接創(chuàng)建Date數(shù)據(jù)類型的實(shí)例變量date并直接將其輸出:
/**
* 創(chuàng)建Date類型的變量 this is date :Sat Dec 06 00:26:54 CST 2008
* 程序運(yùn)行過(guò)程中直接將當(dāng)前的時(shí)間打印出來(lái)
*/
Date date = new Date();
System.out.println("this is date :" + date);
2.將直接實(shí)例化的Date數(shù)據(jù)以String的方式進(jìn)行輸出:
**
* 實(shí)例方法toString() Sat Dec 06 00:26:54 CST 2008
* 其實(shí)效果也是和程序的直接輸出一樣只不過(guò)將輸出的參數(shù)類型改變成為String的類型。
*/
String str_date = date.toString();
System.out.println("this is date_ str :" + str_date);
3.采用DateFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 :DateFormat.getInstance() 輸出 :08-12-6 上午12:26
* DateFormat()方法將日期格式化,格式輸出到當(dāng)前日的分上面。
*/
String str_date_1 = DateFormat.getInstance().format(date);
System.out.println("this is str_date_1 :" + str_date_1);
4.采用SimpleDateFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 :SimpleDateFormat() 輸出 :2008 12 06 00 26 54
* SimpleDateFormat()放法和DateFormat()類似他可以直接制定到當(dāng)前日期的某一階段 例如實(shí)例指定當(dāng)前的秒鐘。
*/
SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH mm ss");
System.out.println("this is SimpleDateFormat :" + time.format(date));
5.采用MessageFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 : MessageFormat() 輸出 :2008-12-06-00-26:54:2654
*/
String dateTime = MessageFormat.format(
"{0,date,yyyy-MM-dd-HH-mm:ss:ms}",
new Object[] { new java.sql.Date(System.currentTimeMillis()) });
System.out.println("this is Message datetime :" + dateTime);
/**
* 方法 : MessageFormat() 輸出 :2008-12-06-00-26:54:2654
*/
String dateTime = MessageFormat.format(
"{0,date,yyyy-MM-dd-HH-mm:ss:ms}",
new Object[] { new java.sql.Date(System.currentTimeMillis()) });
System.out.println("this is Message datetime :" + dateTime);
6.采用SimpleDateFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 : SimpleDateFormat() 輸出 : 2008-12-06
* 類似于前面所講到的SimpleDateFormat()方法、其實(shí)原理都是相同的只不過(guò)在處理過(guò)程中采用別的附加條件。
*
*/
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); // 格式化當(dāng)前系統(tǒng)日期
String dateTime_1 = dateFm.format(new java.util.Date());
System.out.println("this is SimpleDateFormat :" + dateTime_1);
7.采用DateFormat()方法格式化得到你所需要的Date參數(shù):
/**
* 方法 :DateFormat()
* 通過(guò)DateFormat()方法所控制的不同參數(shù)來(lái)顯示當(dāng)前日期時(shí)間
*/
//簡(jiǎn)略的將當(dāng)前日期時(shí)間顯示出來(lái)
DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
System.out.println(shortDateFormat.format(date));
//精確地顯示當(dāng)前日期時(shí)間
DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM);
System.out.println(mediumDateFormat.format(date));
//完全的將當(dāng)前的日期時(shí)間顯示出來(lái)
DateFormat longDateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG);
System.out.println(longDateFormat.format(date));
//全部標(biāo)準(zhǔn)化的將當(dāng)前日期時(shí)間按輸出出來(lái)。
DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL);
System.out.println(fullDateFormat.format(date));
8.同時(shí)可以通過(guò)getTime()方法獲取當(dāng)前日期的時(shí)間:
不過(guò)這樣所得到的Date類型數(shù)據(jù)是以秒來(lái)計(jì)算的、并且是以1970年1月1日為開始的。在聲明數(shù)據(jù)類型時(shí)需要較大存儲(chǔ)空間使用Long數(shù)據(jù)類型或者同等存儲(chǔ)類型數(shù)據(jù)。
/**
* 方法 : getTime() 輸出 : 1228494414199 這個(gè)時(shí)間是按照1970年1月1日開始經(jīng)歷的毫秒數(shù)了、
*/
long str_get = date.getTime();
System.out.println("this is gettime :" + str_get);
然而在使用過(guò)程中許多人卻沒有真正的去了解有關(guān)于Java 數(shù)據(jù)類型中Date數(shù)據(jù)類型.
通常在這使用某一種數(shù)據(jù)類型過(guò)程中并不一定就會(huì)有哦相應(yīng)的數(shù)據(jù)類型,往往存在各種各樣的數(shù)據(jù)類型不斷的更替轉(zhuǎn)換。
1.直接創(chuàng)建Date數(shù)據(jù)類型的實(shí)例變量date并直接將其輸出:
/**
* 創(chuàng)建Date類型的變量 this is date :Sat Dec 06 00:26:54 CST 2008
* 程序運(yùn)行過(guò)程中直接將當(dāng)前的時(shí)間打印出來(lái)
*/
Date date = new Date();
System.out.println("this is date :" + date);
2.將直接實(shí)例化的Date數(shù)據(jù)以String的方式進(jìn)行輸出:
**
* 實(shí)例方法toString() Sat Dec 06 00:26:54 CST 2008
* 其實(shí)效果也是和程序的直接輸出一樣只不過(guò)將輸出的參數(shù)類型改變成為String的類型。
*/
String str_date = date.toString();
System.out.println("this is date_ str :" + str_date);
3.采用DateFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 :DateFormat.getInstance() 輸出 :08-12-6 上午12:26
* DateFormat()方法將日期格式化,格式輸出到當(dāng)前日的分上面。
*/
String str_date_1 = DateFormat.getInstance().format(date);
System.out.println("this is str_date_1 :" + str_date_1);
4.采用SimpleDateFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 :SimpleDateFormat() 輸出 :2008 12 06 00 26 54
* SimpleDateFormat()放法和DateFormat()類似他可以直接制定到當(dāng)前日期的某一階段 例如實(shí)例指定當(dāng)前的秒鐘。
*/
SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH mm ss");
System.out.println("this is SimpleDateFormat :" + time.format(date));
5.采用MessageFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 : MessageFormat() 輸出 :2008-12-06-00-26:54:2654
*/
String dateTime = MessageFormat.format(
"{0,date,yyyy-MM-dd-HH-mm:ss:ms}",
new Object[] { new java.sql.Date(System.currentTimeMillis()) });
System.out.println("this is Message datetime :" + dateTime);
/**
* 方法 : MessageFormat() 輸出 :2008-12-06-00-26:54:2654
*/
String dateTime = MessageFormat.format(
"{0,date,yyyy-MM-dd-HH-mm:ss:ms}",
new Object[] { new java.sql.Date(System.currentTimeMillis()) });
System.out.println("this is Message datetime :" + dateTime);
6.采用SimpleDateFormat()方法格式化或者過(guò)濾所需要的數(shù)據(jù)參數(shù):
/**
* 方法 : SimpleDateFormat() 輸出 : 2008-12-06
* 類似于前面所講到的SimpleDateFormat()方法、其實(shí)原理都是相同的只不過(guò)在處理過(guò)程中采用別的附加條件。
*
*/
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); // 格式化當(dāng)前系統(tǒng)日期
String dateTime_1 = dateFm.format(new java.util.Date());
System.out.println("this is SimpleDateFormat :" + dateTime_1);
7.采用DateFormat()方法格式化得到你所需要的Date參數(shù):
/**
* 方法 :DateFormat()
* 通過(guò)DateFormat()方法所控制的不同參數(shù)來(lái)顯示當(dāng)前日期時(shí)間
*/
//簡(jiǎn)略的將當(dāng)前日期時(shí)間顯示出來(lái)
DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
System.out.println(shortDateFormat.format(date));
//精確地顯示當(dāng)前日期時(shí)間
DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM);
System.out.println(mediumDateFormat.format(date));
//完全的將當(dāng)前的日期時(shí)間顯示出來(lái)
DateFormat longDateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG);
System.out.println(longDateFormat.format(date));
//全部標(biāo)準(zhǔn)化的將當(dāng)前日期時(shí)間按輸出出來(lái)。
DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL);
System.out.println(fullDateFormat.format(date));
8.同時(shí)可以通過(guò)getTime()方法獲取當(dāng)前日期的時(shí)間:
不過(guò)這樣所得到的Date類型數(shù)據(jù)是以秒來(lái)計(jì)算的、并且是以1970年1月1日為開始的。在聲明數(shù)據(jù)類型時(shí)需要較大存儲(chǔ)空間使用Long數(shù)據(jù)類型或者同等存儲(chǔ)類型數(shù)據(jù)。
/**
* 方法 : getTime() 輸出 : 1228494414199 這個(gè)時(shí)間是按照1970年1月1日開始經(jīng)歷的毫秒數(shù)了、
*/
long str_get = date.getTime();
System.out.println("this is gettime :" + str_get);