1. Introduction
The API of the Date class is not easy to internationalize, and most of them are abandoned
java.text.SimpleDateFormat
It is a concrete class that formats and parses dates in a way that is not related to the localeIt allows formatting: date—>text parsing: text—>date
Second, the role
1. Format: Date -> Text
2. Analysis: text -> date
The constructor of the SimpleDateFormat class has and without parameters
SimpleDateFormat sdf = new SimpleDateFormt(“yyyy-MM-dd hh-mm-ss”);
The above parameter is the format, the result of formatting is this format, and the string passed in during parsing must also be in this format
Use parameterized constructor instantiation in general development
The string passed in during parsing must be in accordance with the format declared by the constructor, otherwise an error will be reported
//实例化SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormt("yyyy-MM-dd hh-mm-ss");
//格式化:日期->文本
Date date = new Date();
String str = sdf.format(date);
System.out.println(str);
//解析:文本->日期
Date date = sdf.parse(str);
System.out.println(date);