|
|
Posted on 1/11/2019 3:19:25 PM
|
|
|
|

When we register a website, we often need to fill in personal information, such as name, age, date of birth, etc., when the value of the date of birth on the page is passed to the background, it is a string, and we do need a date type when we store it in the database, in turn, when it is displayed on the page, we need to get the date of birth from the database, at this time the type is the date type, and then we need to convert the date type into a string to display on the page. Java's API provides us with a class called DateForamt that forwards dates and strings to each other. DateForamt is an abstract class, so its subclass SimpleDateFormat is usually used. SimpleDateFormat has 4 constructors, the second is most often used.
The pattern in the constructor is a time mode, and the API explains what pattern it is, as follows
1. Date to string (formatting)
Run the result
2016-10-24 2016-10-24 21:59:06 October 24, 2016 21:59:06
2. String to date (parsing)
Run the result
Mon Oct 24 21:59:06 CST 2016 When converting strings to dates, you need to pay attention to the given pattern must match the given string format, otherwise java.text.ParseException will be thrown, for example, the following one is wrong, the string does not give the time, minutes, seconds, then SimpleDateFormat of course cannot give you the value of the time, minutes, seconds out of thin air
Run error
However, a given pattern is fine if it has fewer strings
Run the result
Mon Oct 24 00:00:00 CST 2016 It can be seen that the time, minute, and second are 0 and are not parsed, which is okay.
3. Enter the date and convert it to milliseconds: use the Date method ()
code
Run screenshot:
|
Previous:A certain treasure 399 yuan Douyin shooting 106 skillsNext:Silicon Valley, Chuan Zhi Dark Horse and java, Python, big data, php, front-end video tutorials
|