在之前的學(xué)習(xí)中,我寫(xiě)過(guò)一篇關(guān)于字符串格式化的,就主要設(shè)計(jì)到了時(shí)間以及日期的各種格式化顯示的設(shè)置,其主要時(shí)通過(guò)String類的fomat()方法實(shí)現(xiàn)的.
我們可以通過(guò)使用不同的轉(zhuǎn)換符來(lái)實(shí)現(xiàn)格式化顯示不同的時(shí)間以及日期信息,但我們了解到,時(shí)間以及日期的轉(zhuǎn)換符實(shí)在是太多了,導(dǎo)致我們無(wú)法十分方便的在需要的時(shí)候格式化出想要的日期時(shí)間輸出格式.
然而在學(xué)習(xí)過(guò)程中,我們了解到類是可以相互調(diào)用的,以及靜態(tài)方法是可以跨類使用的,,所以,通過(guò)本文,將構(gòu)建一個(gè)顯示時(shí)間日期的工具類,定義幾個(gè)常用的日期時(shí)間格式,之后我們?cè)谑褂玫臅r(shí)候,只需要調(diào)用相應(yīng)的方法即可.
**在2019項(xiàng)目下創(chuàng)建專門(mén)的包My_tools(工具類包),并在包FormatTimetool工具類.
- 源代碼
package My_tools; // 創(chuàng)建包,我的工具類
import java.util.Date;
import java.util.Locale;
/**
* @outhor xiaoshe
* @date 2019/4/5 - @time 1:22
* 創(chuàng)建格式時(shí)間的工具類.
* 之前學(xué)到的格式化日期時(shí)間,,而我們知道,日期時(shí)間的表示,我們需要經(jīng)常使用到,所以這里抽出為靜態(tài)方法,作為工具類來(lái)使用.
*/
public class FormatTimetool {
static Date date = new Date(); // 實(shí)例化一個(gè)靜態(tài)的Date對(duì)象.
//將時(shí)間按24小時(shí)制,分:秒格式輸出
public static void ShowTime_colon(){
System.out.println(String.format("%tR",date));
}
//將時(shí)間按24小時(shí)制,以時(shí):分:秒的格式完整輸出
public static void ShowAlltime_colon(){
System.out.println(String.format("%tT",date));
}
//將時(shí)間按減號(hào),以:年-月-日的格式輸出.
public static void Showdate_Minus(){
System.out.println(String.format("%tF",date));
}
// 將時(shí)間按斜杠,以月/日/年的格式輸出
public static void Showdate_Slash(){
System.out.println(String.format("%tD",date));
}
// 將年-月-日的格式的日期和時(shí):分的格式的時(shí)間組合輸出
public static void SdateTime_mc(){
System.out.println(String.format("%tF",date)+" "+String.format("%tR",date));
}
// 將年-月-日的格式的日期和時(shí):分:秒的格式的時(shí)間組合輸出
public static void SdateAllTime_mc(){
System.out.println(String.format("%tF",date)+" "+String.format("%tT",date));
}
//將月/日/年的格式的日期和時(shí):分的格式的時(shí)間組合輸出
public static void Sdatetime_sc(){
System.out.println(String.format("%tD",date)+" "+String.format("%tR",date));
}
//英文下的的星期幾全稱輸出
public static void ShowWeek_e(){
System.out.println(String.format(Locale.ENGLISH,"%tA",date));
}
//輸出中文星期幾
public static void ShowWeek(){
System.out.println(String.format(Locale.CHINA,"%tA",date));
}
// 按固定格式輸出: x年x月x日;x時(shí)x分;
public static void Sdate_china(){
String ayear = String.format("%tY",date);
String amonth = String.format("%tm",date);
String aday = String.format("%te",date);
System.out.println(ayear+"年"+amonth+"月"+aday+"日");
}
//按固定格式輸出:x時(shí)x分
public static void Stime_china(){
String ahour = String.format("%tH",date);
String aminute = String.format("%tM",date);
System.out.println(ahour+"時(shí)"+aminute+"分");
}
}
- 測(cè)試
我們?nèi)我庑陆ㄒ粋€(gè)類,調(diào)用FormatTimetool工具類的方法試試.
public static void main(String[] args) { // 主方法。
//顯示時(shí)間的兩種格式
FormatTimetool.ShowTime_colon(); // 時(shí):分
FormatTimetool.ShowAlltime_colon(); // 時(shí):分:秒
//顯示日期的兩種格式
FormatTimetool.Showdate_Minus(); // 年-月-日
FormatTimetool.Showdate_Slash(); // 月/日/年
// 三種顯示日期時(shí)間的格式
FormatTimetool.SdateTime_mc(); // 年-月-日 時(shí):分
FormatTimetool.SdateAllTime_mc(); // 年-月-日 時(shí):分:秒
FormatTimetool.Sdatetime_sc(); // 月/日/年 時(shí):分
//兩種自定義日期時(shí)間輸出
FormatTimetool.Sdate_china(); // x年x月x日
FormatTimetool.Stime_china(); // x時(shí)x分
// 顯示星期的兩種格式
FormatTimetool.ShowWeek(); // 中文星期幾
FormatTimetool.ShowWeek_e(); // 英文星期全稱
}
-
結(jié)果
成功顯示了各種不同的時(shí)間日期格式化輸出.
在編寫(xiě)程序的時(shí)候,我們通常會(huì)將一些會(huì)多此重復(fù)使用到的公用方法單獨(dú)抽出構(gòu)建成為工具類,以達(dá)到方便我們編寫(xiě)程序,減少多余的重復(fù)性操作,在之后,我們也可以將編寫(xiě)的功能模塊化,使其更具通用性.在需要使用的時(shí)候,只需要簡(jiǎn)單的調(diào)用就行.
工具類系列將會(huì)記錄我個(gè)人的工具類的構(gòu)建過(guò)程,之后會(huì)持續(xù)更新自己將會(huì)使用到的一些工具類的構(gòu)建.
更新時(shí)間:
2019-4-5
3:07