[java工具类01]__构建格式化输出日期和时间的工具类

在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的.

我们可以通过使用不同的转换符来实现格式化显示不同的时间以及日期信息,但我们了解到,时间以及日期的转换符实在是太多了,导致我们无法十分方便的在需要的时候格式化出想要的日期时间输出格式.

然而在学习过程中,我们了解到类是可以相互调用的,以及静态方法是可以跨类使用的,,所以,通过本文,将构建一个显示时间日期的工具类,定义几个常用的日期时间格式,之后我们在使用的时候,只需要调用相应的方法即可.
*
在2019项目下创建专门的包My_tools(工具类包),并在包FormatTimetool工具类.

  • 源代码
package My_tools; // 创建包,我的工具类

import java.util.Date;
import java.util.Locale;

/**
 * @outhor xiaoshe
 * @date 2019/4/5  - @time 1:22
 * 创建格式时间的工具类.
 * 之前学到的格式化日期时间,,而我们知道,日期时间的表示,我们需要经常使用到,所以这里抽出为静态方法,作为工具类来使用.
 */
public class FormatTimetool {

    static Date date = new Date();  // 实例化一个静态的Date对象.

    //将时间按24小时制,分:秒格式输出
    public static void ShowTime_colon(){
        System.out.println(String.format("%tR",date));
    }
    //将时间按24小时制,以时:分:秒的格式完整输出
    public static void ShowAlltime_colon(){
        System.out.println(String.format("%tT",date));
    }
    //将时间按减号,以:年-月-日的格式输出.
    public static void Showdate_Minus(){
        System.out.println(String.format("%tF",date));
    }
    // 将时间按斜杠,以月/日/年的格式输出
    public static void Showdate_Slash(){
        System.out.println(String.format("%tD",date));
    }
    // 将年-月-日的格式的日期和时:分的格式的时间组合输出
    public static void SdateTime_mc(){
        System.out.println(String.format("%tF",date)+" "+String.format("%tR",date));
    }
    // 将年-月-日的格式的日期和时:分:秒的格式的时间组合输出
    public static void SdateAllTime_mc(){
        System.out.println(String.format("%tF",date)+" "+String.format("%tT",date));
    }
    //将月/日/年的格式的日期和时:分的格式的时间组合输出
    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时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时x分
    public static void Stime_china(){
        String ahour = String.format("%tH",date);
        String aminute = String.format("%tM",date);
        System.out.println(ahour+"时"+aminute+"分");
    }
}
  • 测试
    我们任意新建一个类,调用FormatTimetool工具类的方法试试.
 public static void main(String[] args) {  // 主方法。
        //显示时间的两种格式
        FormatTimetool.ShowTime_colon(); // 时:分
        FormatTimetool.ShowAlltime_colon(); // 时:分:秒
        //显示日期的两种格式
        FormatTimetool.Showdate_Minus(); // 年-月-日
        FormatTimetool.Showdate_Slash(); // 月/日/年
        // 三种显示日期时间的格式
        FormatTimetool.SdateTime_mc(); // 年-月-日 时:分
        FormatTimetool.SdateAllTime_mc(); // 年-月-日 时:分:秒
        FormatTimetool.Sdatetime_sc(); // 月/日/年 时:分
        //两种自定义日期时间输出
        FormatTimetool.Sdate_china(); // x年x月x日
        FormatTimetool.Stime_china(); // x时x分
        // 显示星期的两种格式
        FormatTimetool.ShowWeek(); // 中文星期几
        FormatTimetool.ShowWeek_e(); // 英文星期全称
    }
  • 结果
    2019-4-5-01.png

成功显示了各种不同的时间日期格式化输出.
***
在编写程序的时候,我们通常会将一些会多此重复使用到的公用方法单独抽出构建成为工具类,以达到方便我们编写程序,减少多余的重复性操作,在之后,我们也可以将编写的功能模块化,使其更具通用性.在需要使用的时候,只需要简单的调用就行.

工具类系列将会记录我个人的工具类的构建过程,之后会持续更新自己将会使用到的一些工具类的构建.


更新时间:
2019-4-5
3:07

猜你喜欢

转载自www.cnblogs.com/gemuxiaoshe/p/10657828.html