Java Basics-Date formatting DateFormat class Introduction
Yun Zhengjie
Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.
I. Overview of the DateFormat class
DateFormat is an abstract class of date/time formatting subclasses (descriptions cannot be instantiated directly), and it formats and resolves dates or times in a language-independent manner. Date/time formatting subclasses (such as SimpleDateFormat) allow formatting (that is, date-and text), parsing (text-to-date), and normalization. Represents a date as an Date
object, or as the number of milliseconds from GMT (Greenwich Mean Time) January 1, 1970 00:00:00 This moment.
two. SimpleDateFormat class overview
SimpleDateFormat
is a specific class that formats and resolves dates in a language-related way. It allows formatting (date-and text), parsing (text-to-date), and normalization.
Three. How to format dates
1> Create SimpleDateFormat Object
In the class construction method, the string is written in many date formats, and the format can be customized, but the following rules are required:
2>. SimpleDateFormat to format dates by calling format
1 PackageCn.org.yinzhengjie.Demo;2 3 ImportJava.text.SimpleDateFormat;4 Importjava.util.Date;5 6 Public classSimpledateformatdemo {7 8 Public Static voidMain (string[] args) {9SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day HH point mm minutes ss seconds! ");TenString date = Sdf.format (NewDate ()); One System.out.println (date); A - } - the } - - - /* + The result of the above code execution is as follows: - April 18, 2018 17 O ' 01分钟16秒! + */
Four. String converted to date object
1> Create a SimpleDateFormat object
In the construction method, specify the date format.
2>. Subclass object, calling method parse, passing string, returns date.
1 PackageCn.org.yinzhengjie.Demo;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 Public classSimpledateformatdemo {8 9 Public Static voidMain (string[] args)throwsParseException {Ten //incoming parameters require a custom format OneSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); A //the incoming format needs to be consistent with the format you define, otherwise it will be an error! You need to handle exceptions when calling the Parse method. -Date date = Sdf.parse ("1990-05-19"); - System.out.println (date); the } - - } - + - /* + The result of the above code execution is as follows: A Sat may 00:00:00 gmt+08:00 1990 at */
Java Basics-Date formatting DateFormat class Introduction