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

    1.1. 与FoosunCMS 3.1.0930整合 基于WordPaster-asp-CKEditor4.x示例 下载地址:http://www.ncmem.com/download/WordPas ...

  2. Remote Debugging (1)

    The client/server design of the Java debugger allows you to launch a Java program from computer on y ...

  3. GetWindowRect

    示例代码: CRect rect; GetDlgItem(IDC_STATIC_VIEW)->GetWindowRect(&rect); int width=rect.Width(); ...

  4. JS鼠标滚动插件scrollpath使用介绍

    JS鼠标滚动插件scrollpath:在这个插件中首先要引人的JS是jQuery,因为后面的JS都是基于它的.再者需要引入的是jquery.scrollpath.js.scrollpath.css还有 ...

  5. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

  6. hdu 3191 次短路的长度和个数

    http://acm.hdu.edu.cn/showproblem.php?pid=3191 求次短路的长度和个数 相关分析在这里http://blog.csdn.net/u012774187/art ...

  7. zoj2607

    题意:如左图,给定A,B,C,D的面积分别为大于等于a,b,c,d,求最小的面积 思路:因为a,b肯定有一个是满的(不然还可压缩到更小),同理,ac,bd,cd都只有一个是满的,所以有可能是对角满的, ...

  8. CSS 基础 例子 定位及z-index

    position 属性指定了元素的定位类型. position 属性的四个值: static    不设置默认为该值,即没有定位,元素出现在正常的流中.不能使用top,bottom,left,righ ...

  9. 蒲公英: 一个提供App 存储、分发、Bug管理的网站

    一.蒲公英内测应用, https://www.pgyer.com/ 内测应用,仅需两步: 将应用上传到网站,生成安装链接和二维码 用户在手机上打开安装链接,或扫码二维码,即可开始安装 二.蒲公英Bug ...

  10. .NET Core下开源任务调度框架Hangfire的Api任务拓展(支持秒级任务)

    HangFire的拓展和使用 看了很多博客,小白第一次写博客. 最近由于之前的任务调度框架总出现问题,因此想寻找一个替代品,之前使用的是Quartz.Net,这个框架方便之处就是支持cron表达式适合 ...