SimpleDateFormat is a class that handles date format conversion.
Official API_1.8 Screenshot of SimpleDateFormat inheriting from DateFormate:
The constructor of SimpleDateFormat is as follows:
Format definition in SimpleDateFormat, commonly used is circled in red frame:
Chinese explanation:
y : year
M: The month of the year
D: Days in the year
d: days in the month
w: Weeks in the year
W: Weeks in the month
a: Up/down/morning
H: Number of hours in a day (0-23)
h: hours in a day (0-12)
m: Minutes of hours
s: seconds in minutes
S: milliseconds
SimpleDateFormat method:
Methods inherited from DateFormate:
SimpleDateFormat common methods and common format definition examples:
package com.lanhuigu.java.format;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class FormatTest {public static void main(String[] args) throws ParseException {// *******************1.(format,parse)Most common method example******************* System.out.println("---------------------------");// Format SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Time Date date1 = new Date();System.out.println("Time before operation:" + date1);// Date type time-》Convert to defined format-》String type time/* * Note: format(Date date) This method comes from the parent class of SimpleDateFormat */String str1 = sdf1.format(date1);System.out.println("String type time:" + str1);// String type time-》Convert to defined format-》Date type time DateF1 = sdf1.parse(str1);System.out.println("Date type time:" + dateF1);// ****************2.About common format analysis******************* System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ : milliseconds*/// Note that in order to save trouble, this place puts all the commonly used ones in this place and print them together to see the effect. // In actual use, convert the corresponding format according to the needs SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd,w,W,a,HH:mm:ss,SS");String str2 = sdf2.format(new Date());System.out.println("Date type time:" + str2);System.out.println("String type time:" + sdf2.parse(str2));// ***************2. Analysis of constructor usage skills******************* System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Date()));}}Program running results:
Summarize
Regarding SimpleDateFormate, you need to know how to use common methods under different parameters, as well as common formats and constructor abbreviation methods.
The above is all about this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!