Commonly used class library Java.text.SimpleDateFormat

Detailed explanation of simpleDateFormat date format settings in common date format classes in Java

SimpleDateFormat class overview-JDK11API

Class SimpleDateFormat
java.lang.Object
 java.text.Format
  java.text.DateFormat
   java.text.SimpleDateFormat
implements all interfaces Serializable, Cloneable
public class SimpleDateFormat extends DateFormat


SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive way. It allows formatting (date → text), parsing (text → date) and normalization.
SimpleDateFormat allows you to start by choosing any user-defined pattern for the date time format. However, it is recommended that you create a date-time format with two kinds of getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with the default format mode. You can use the applyPattern method to modify the format pattern as needed.


Date and time pattern The
date and time format is specified by the date and time pattern string. In the date and time pattern string, the unquoted letters from'A' to'Z' and from'a' to'z' are interpreted as pattern letters that represent components of the date or time string. You can use single quotes (') to quote text to avoid interpretation. "''" stands for single quotation mark. All other characters are not interpreted; they are simply copied into the output string during the formatting process, or matched with the input string during the parsing process.
The following pattern letters are defined (all other characters from'A' to'Z' and from'a' to'z' are reserved):
Format characters defined in SimpleDateFormat

Common methods in SimpleDateFormat

Three commonly used construction methods

SimpleDateFormat()    // 构造一个 SimpleDateFormat使用默认模式和日期格式符号默认 FORMAT区域设置。 
SimpleDateFormat​(String pattern)  // 使用给定的模式和日期格式符号构造 SimpleDateFormat 。 
SimpleDateFormat​(String pattern, Locale locale)  // 使用给定模式和给定语言环境的默认日期格式符号构造 SimpleDateFormat 。 

Two ways to view date format

1.public String toLocalizedPattern()
作用:返回描述此日期格式的本地化模式字符串。 
结果:描述此日期格式的本地化模式字符串。
使用:
SimpleDateFormat format = new SimpleDateFormat();
System.out.println(format.toLocalizedPattern());
打印结果:
y/M/d ah:mm

2.public String toPattern()
作用:返回描述此日期格式的模式字符串。 
结果:描述此日期格式的模式字符串。 
使用:
SimpleDateFormat format1 = (SimpleDateFormat) DateFormat.getDateInstance();
System.out.println(format1.toPattern());
打印结果:
y年M月d日

Two methods of parsing string text to generate date

1.parse​(String source)   // public Date parse​(String source) throws ParseException
该方法继承其父类DateFormat,作用:从给定字符串的开头解析文本以生成日期。 该方法可能不使用给定字符串的整个文本。
参数 source - 应该解析其开头的 String 
结果 从字符串解析的 Date 。 
异常 ParseException - 如果无法解析指定字符串的开头。
使用:
SimpleDateFormat format1 = (SimpleDateFormat) DateFormat.getDateInstance();
// 格式为 y年M月d日
Scanner input = new Scanner(System.in);
String text1 = input.next();
Date date1 = format1.parse(text1);
System.out.println(date1);
打印结果:
输入  20201221日
输出  Mon Dec 21 00:00:00 CST 2020
有正确的输出,说明该输入的文本确实已经被成功解析为date1对象

2.parse​(String text, ParsePosition pos)   // public Date parse​(String text, ParsePosition pos)
作用:解析字符串中的文本以生成 Date 。 
参数; text - A String ,其中一部分应该被解析。  pos - 具有 ParsePosition索引和错误索引信息的 ParsePosition对象。 
结果:从字符串解析的Date 。 如果出现错误,则返回null。
异常:NullPointerException - 如果 text或 pos为空。  
//该方法尝试从pos给出的索引处开始解析文本。 
//如果解析成功,则在使用最后一个字符之后将索引pos更新为索引(解析不一定使用直到字符串末尾的所有字符),并返回解析的日期。 更新的pos可用于指示下一次调用此方法的起点。 
//如果发生错误,则不更改索引pos ,将错误索引pos设置为发生错误的字符的索引,并返回null。 
//此解析操作使用calendar生成Date 。 在解析之前,所有calendar的日期时间字段都是cleared ,并且calendar的日期时间字段的默认值用于任何缺少的日期时间信息。
// 例如,如果解析操作没有给出年份值,则解析的Date的年份值为1970, 其中 Date为GregorianCalendar 。 可能会覆盖TimeZone值,具体取决于给定的模式和text的时区值。 之前通过调用setTimeZone设置的任何TimeZone值可能需要恢复以进行进一步操作。
使用:
Scanner input = new Scanner(System.in);
String text2 = input.next();
ParsePosition position = new ParsePosition(3);
Date date2 = format1.parse(text2,position);
System.out.println(date2);
输入: 今天是2021125日
输出: Mon Jan 25 00:00:00 CST 2021
由此可见,date2创建成功,即输入的文本text2成功从字符串数组下标为  index= 3 实际上是第4个字符处开始解析格式是否符合format1的 y年M月d日 的格式

Hands-on code verification of various formats

Through several default format methods in DateFormat

DateFormat里提供的几个默认格式的方法的调用:

SimpleDateFormat format1 = (SimpleDateFormat) DateFormat.getDateInstance();
SimpleDateFormat format2 = (SimpleDateFormat) DateFormat.getDateTimeInstance();
SimpleDateFormat format3 = (SimpleDateFormat) DateFormat.getTimeInstance();
Date date = new Date();
System.out.println(format1.format(date));
System.out.println(format1.toPattern());
System.out.println(format2.format(date));
System.out.println(format1.toPattern());
System.out.println(format3.format(date));
System.out.println(format3.toPattern());
打印结果:
2021124日     y年M月d日  //  DateFormat.getDateInstance()
2021124日 下午5:18:20  y年M月d日 ah:mm:ss   //DateFormat.getDateTimeInstance();
下午5:18:20   ah:mm:ss  // DateFormat.getTimeInstance()

Custom date format

SimpleDateFormat里定义的日期格式字符解释:
y	--- 	年份
Y	--- 	按周计算的年,与y的区别在于一年的第一周和最后一周,一般不用Y
M	---	月份Month in year (context sensitive) 
L	---	月份Month in year (standalone form)  暂时不知道M与L的区别,测试两者结果一致
w	---	一年中的周数即当前年份的第几周
W	---	当前月中的周数即第几周
D	---	一年中的天数即当前年的第几天
d	---	一月中的天数即当前月份的第几天
F 	---	是当前月份的第几周
E	---	当前日期是星期几的名字   满足local比如当前是CHINA 输出的名字是 汉字 例如“周日”   
u	---	当前日期在当前周的天数的序号, 取指范围 “1~7” 对应周一到周日
a	---	按照am/pm的方式显示当前处于am还是pm,和 h 连用 变成12小时 制,输出时间范围 0~11“” 支持local 在CHINA 下显示 中文 “上午”/“下午”
H	---	0~23  24小时制表示时间
k	---	1~24 24小时制表示时间
K	---	0~11 am/pm 12小时制表示时间
h	---	1~12 am/pm 12小时制表示时间
m	---	minute 表示分钟
s	---	second 表示秒
S	---	Millisecond 表示毫秒
z	---	Time zone 表示时区
Z	---	Time zone 表示时区
X	---	Time zone 表示时区    Z,X,z三者的区别在于时区的算法不同,此处不深究
G	---	Era designator  选择器,当格式中涉及到的字母数多的时候可以简写yyyy MM 为y M 等等。


SimpleDateFormat的三种常用构造方法:

1.SimpleDateFormat()    生成该方法下的默认格式日期:
SimpleDateFormat format = new SimpleDateFormat();
System.out.println(format.format(date));
System.out.println(format.toPattern());
打印结果: 
2021/1/24 下午5:58
y/M/d ah:mm      // 这就是 SimpleDateFormat() 方法的默认格式

2.SimpleDateFormat​(String pattern)  
//构造一个 SimpleDateFormat使用给定的模式和默认的默认日期格式符号 FORMAT区域设置。 
此方法生成的日期格式为自定义的 字符串格式 pattern

SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd ahh:mm:ss");
System.out.println(format4.format(date));
打印结果:
2021-01-24 下午06:10:27

3.SimpleDateFormat​(String pattern, Locale locale)   
//使用给定模式和给定语言环境的默认日期格式符号构造 SimpleDateFormat 。  
SimpleDateFormat format6 = new SimpleDateFormat("yyyy年MM月dd日是该年的第D天第w周,是L月的第W周的第u天E,该天现在时间为ah时mm分ss秒", Locale.CHINA);
System.out.println(format6.format(date));
打印结果:
20210125日是该年的第25天第5周,是1月的第5周的第1天周一,该天现在时间为上午121114秒

此时若把 Local.CHINA 改为 Local.US , 则打印结果变为:
20210125日是今年的第25天第5周,是1月的第5周的第1天Mon,该天现在时间为AM12时1114秒
此处注意 我当前的时间为0:11,由此可以验证h的取指是从1~12下面我们单独改时间试试 H k K

H:
SimpleDateFormat format7 = new SimpleDateFormat("当前时间为HH时mm分ss秒",Locale.CHINA);
System.out.println(format7.format(date));
打印结果:
当前时间为001705秒
可见 H 是 正常的24小时制显示时间

k:
SimpleDateFormat format8 = new SimpleDateFormat("当前时间为kk时mm分ss秒",Locale.CHINA);
System.out.println(format8.format(date));
打印结果:
当前时间为242701秒
可见 k 是 1~2424小时制,0时表达为24时

K:
SimpleDateFormat format9 = new SimpleDateFormat("当前时间为a KK时mm分ss秒",Locale.CHINA);
System.out.println(format9.format(date));
打印结果:
当前时间为上午 003230秒
可见 K 是 0~11 12小时制, 此时需要加上 a 来判断是am还是pm/ 上午还是下午

h:
SimpleDateFormat format10 = new SimpleDateFormat("当前时间为a hh时mm分ss秒",Locale.CHINA);
System.out.println(format10.format(date));
打印结果:
当前时间为上午 125108秒
可见 h 是 1~12 12小时制,此时需要加上 a 来判断是am还是pm/ 上午还是下午

Guess you like

Origin blog.csdn.net/qq_40694640/article/details/113093031