“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
preface
Hello everyone, I'm GW_gw. I'm glad to learn and progress with you.Copy the code
The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.
Abstract
This article focuses on the role and constructor of the SimpleDateFormat class.Copy the code
3. The SimpleDateFormat classes
3.1 Purpose of the SimpleDateFormat class
Although we use DateFormat class has been able to carry out date or time formatting, but sometimes does not meet our needs, then we can use DateFormat subclass SimpleDateFormat class, to carry out custom format date or time formatting.
SimpleDateFormat is a concrete class that formats and parses dates in a locale-specific manner. It allows formatting (date -> text), parsing (text -> date), and normalization. The SimpleDateFormat class is basically just two formatting methods, which we’ll talk about below.
3.2 Constructor of the SimpleDateFormat class
SimpleDateFormat has four constructors, which we’ll look at in turn.
Before we introduce the four constructors, let’s look at some pattern letters.
3.2.1 Mode letters:
The letter Date or time element The sample G
Era markers AD
y
years 1996
;96
yyyy yyM
Month of the year July
;Jul
;07
w
Weeks of the year 27
W
The number of weeks in a month 2
D
Days of the year 189
d
Days of the month 10
F
Week of the month 2
E
Days of the week Tuesday
;Tue
a
Am/PM PM
H
Number of hours in a day (0-23) 0
k
Number of hours in a day (1-24) 24
K
Hours in AM/PM (0-11) 0
h
Hours in AM/PM (1-12) 12
m
The number of minutes in an hour 30
s
The number of seconds in minutes 55
S
Number of milliseconds 987
3.2.2 SimpleDateFormat ()
SimpleDateFormat constructs SimpleDateFormat with the default schema and date format notation for the default locale.
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat();
Date date1 = new Date();
String format1 = simpleDateFormat1.format(date1);
System.out.println(format1);
Copy the code
3.2.3 SimpleDateFormat (String pattern)
SimpleDateFormat(String Pattern) Constructs SimpleDateFormat from the given schema and the date format symbol of the default locale.
/ / SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(" YYYY-MM-DD HH: MM :ss"); / / SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(" YYYY-MM-DD HH: MM :ss"); Date date2 = new Date(); String format2 = simpleDateFormat2.format(date2); System.out.println(format2);Copy the code
Here are some common patterns:
summary
Today we will first introduce here, tomorrow we continue the SimpleDateFormat class learning, the reason for two to introduce is to write more detailed, I hope to help readers.