Explain the dateformat and simpledateformat classes for formatting dates in Java

DateFormat 1. Dateformat introduction the function of dateformat is to format and parse "date / time". In fact, it is a date formatting tool. It can help us format the date and then convert the date into the string string we want for us to use. However, the formatting function of dateformat is limited and is not as powerful as simpledateformat; But dateformat is a parent of simpledateformat. So, let's first have an overall understanding of dateformat, and then learn simpledateformat. Dateformat is used to format date. It supports four formatting styles, including full, long, medium and short: (01) dateformat Short is a complete number, such as 12.13 52 or 3:30pm (02) dateformat Medium is long, such as Jan 121952 (03) dateformat Long is longer, such as January 121952 or 3:30:32pm (04) dateformat Full is fully specified, such as Tuesday, April 12, 1952 ad or 3:30:42pm PST. The definition of dateformat is as follows: public abstract class numberformat extensions format {} the default constructor of the function interface of dateformat: dateformat()

Non constructor:

Note: dateformat is an abstract class. When we get the dateformat instance through getinstance(), getdateinstance() and getdatetimeinstance() of dateformat; It is actually the simpledateformat object returned. The following functions are actually returned simpledateformat objects.

These functions are in simpledateformat Definitions in Java are as follows:

Through the above code, we can further realize that the function of dateformat is to format date; Help us convert date to the string we need. Dateformat provides very limited functions. It can only support four formats: full, long, medium and short. Moreover, when we get the dateformat instance, it is actually the returned simpledateformat object.

2. Dateformat instance next, let's learn the common APIs of using dateformat through examples. The source code is as follows (dateformattest. Java):

Operation results:

OK。 So far, the learning of dateformat is over. Next, let's start to learn simpledateformat, which is the key to formatting date.

SimpleDateFormat 1. Simpledateformat introduction simpledateformat is a tool for formatting date and parsing date strings. Its most common use is to format the date according to the specified format, and then we can use the string obtained after formatting the date. More strictly, simpledateformat is a concrete class that formats and parses dates in a locale related manner. It allows formatting (date - > text), parsing (text - > date), and normalization. Constructor for simpledateformat: constructor

Non constructor

Simpledateformat simple demonstration:

2. Simpledateformat related format description 2.1 date and time mode date and time format are specified by date and time mode string. In date and time pattern strings, the unquoted letters' a 'to' Z 'and' a 'to' Z 'are interpreted as pattern letters to represent date or time string elements. Text can be enclosed in single quotation marks (') to avoid interpretation. '' indicates a single quotation mark. All other characters are not interpreted; They are simply copied to the output string during formatting or matched to the input string during parsing. The following mode letters are defined (all other characters' a 'to' Z 'and' a 'to' Z 'are reserved):

Operation results:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>