SimpleDateFormat in Java USES detail
- 2020-06-15 09:04:43
- OfStack
public class SimpleDateFormat extends DateFormat
SimpleDateFormat is a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date -) > (text), (text - > date) and standardization.
SimpleDateFormat allows you to start in any user-specified manner for date-time formatting. However, you want to create a date-time formatter using getTimeInstance, getDateInstance, or getDateTimeInstance from DateFormat. Each class method returns a date/time formatter initialized with the default formatting. You can modify the formatting using the applyPattern method as needed.
Inheritance of SimpleDateFormat functions:
Java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
Here's a small example:
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat Function syntax:
G Chronological marker
y years
M month
d day
h when In the morning or afternoon (1~12)
H when in 1 In the day (0~23)
m points
s seconds
S ms
E week
D 1 Day of the year
F 1 What day of the month is it
w 1 Week of the year
W 1 Week of the month
a In the morning / In the afternoon markers
k when in 1 In the day (1~24)
K when In the morning or afternoon (0~11)
z The time zone
*/
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy years MM month dd day HH when mm points ss seconds ");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Is equivalent to now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy years MM month dd day HH when mm points ss seconds E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"1 The first of the year D day 1 The first year w A few weeks 1 The first month W A few weeks in 1 In the day k when z The time zone ");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}
}
Effect:
December 16, 2004 17:24 minutes 27 seconds
04/12/16 17:24
2004-12-16 17:24:27
December 16, 2004 17:24:27 SEC Thursday
The 351st day of the year The third week of the 51st week of the year is at 17:00 CST time zone of the day
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
Here is JavaBean:
public class FormatDateTime {
public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy years MM month dd day HH when mm points ss seconds E ");
return myFmt.format(dt);
}
public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy years MM month dd day HH when mm points ");
return myFmt.format(dt);
}
public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}
}
main test results called:
December 16, 2004 17:38:00 26 SEC Thursday
At 17:38 on December 16, 2004
17 38 26 0965
04/12/16 17:38