在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过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

3:07

[java工具类01]__构建格式化输出日期和时间的工具类的更多相关文章

  1. HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)

    HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...

  2. Java知识回顾 (5)数组、日期与时间, StringBuffer和StringBuilder

    一.数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. dataType[] arrayRefVar; // 首选的方法 或 dataType arra ...

  3. Java String 函数常用操作 & format() 格式化输出,代码详解

    package _String_; import java.util.*; import java.math.*; import java.lang.*; public class _Strings ...

  4. Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化

    一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...

  5. thinkphp 5 前台格式化输出日期

    thinkphp格式化输出 {$time|strtotime|date="Y年m月d日",###}   $time 是日期字符串,一般后台的时间是"Y-m-d h:i:s ...

  6. string.Format 格式化输出日期

    string.Format("{0:d}",System.DateTime.Now) 结果为:2009-3-20 (月份位置不是03) string.Format("{0 ...

  7. JAVA 利用SimpleDateFormat将String转换为格式化的日期

    1. /** * 使用用户格式提取字符串日期 * * @param strDate 日期字符串 * @param pattern 日期格式 * @return */ public static Dat ...

  8. thinkphp前台html格式化输出日期

    输出格式: {$time|strtotime|date="Y年m月d日",###} 格式说明: $time 是日期字符串,一般后台的时间是"Y-m-d H:i:s&quo ...

  9. ActionScript 3.0日期与时间管理(Date类)

    ,6)); var now_1:Date=new Date(); trace(now_1.getHours());    /*输出结果会根据设置和测试时间不同而有                    ...

随机推荐

  1. java基础-day20

    第09天 IO流 今日内容介绍 u  File类 u  字符流与字节流 第1章   File类 1.1      File概述 打开API,搜索File类.阅读其描述:File文件和目录路径名的抽象表 ...

  2. python生成器实例

    生成器是一种特殊的迭代器,它有yield语句 #coding:utf-8def fibs(max): n,a,b = 0,0,1 while n < max: yield b a , b = b ...

  3. POJ2061 Subsequence 2017-05-25 19:49 83人阅读 评论(0) 收藏

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14709   Accepted: 6210 Desc ...

  4. Poj2296

    题意:给定n个点,然后在每个点在一个正方形的上边或者下边的中点,并且所有的正方形等大且不能重叠.求正方形最大的边长是多少. 思路:很明显的二分边长+判定.不过判定要用到2-sat,算是2-sat的入门 ...

  5. ClientDataSet 心得

    1.   与TTable.TQuery一样,TClientDataSet也是从TDataSet继承下来的,它通常用于多层体系结构的客户端.很多数据库应用程序都用了BDE,BDE往往给发布带来很大的不便 ...

  6. python threading模块2

    Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...

  7. Flask restful API如何解决跨站请求问题

    如果像下面这样只是在return的response添加header是不行的: response = make_response(jsonify(response=get_articles(ARTICL ...

  8. kotlin面向对象-笔记

  9. C#之WinForm设置控件居中

    简单阐述 在C#的WinForm里面,原生控件是没有居中属性的,故通过重写OnResize(EventArgs e)方法,通过计算,重新定位控件位置. 以Label控件为例 (1)将label的Aut ...

  10. 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView

    [源码下载] 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView 作者:webabcd 介绍背水一战 Windows 1 ...