When we register the website, often need to fill in personal information, such as name, age, date of birth, etc., the value of the date of birth on the page is passed to the background is a string, and we do need a date type in the database, in turn, when displayed on the page, need to get the date of birth from the database, At this point the type is a date type, and then you need to convert the date type to a string to display on the page, and the Java API gives us the class Dateforamt that the date and string are transported to each other. Dateforamt is an abstract class, so it is usually used by its subclass SimpleDateFormat. SimpleDateFormat has 4 constructors, the most commonly used is the second one.
The pattern in the constructor is the time pattern, what is the mode, the API is described in the following
1. Date to string (formatted)
1 PackageCom.test.dateFormat;2 3 ImportJava.text.SimpleDateFormat;4 Importjava.util.Date;5 6 Importorg.junit.Test;7 8 Public classdate2string {9 @TestTen Public voidTest () { OneDate Date =NewDate (); ASimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); - System.out.println (Sdf.format (date)); -SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); the System.out.println (Sdf.format (date)); -SDF =NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); - System.out.println (Sdf.format (date)); - } +}
1 2016-10-242 2016-10-24 21:59:063 October 24, 2016 21:59:06
2. String to date (parsing)
1 PackageCom.test.dateFormat;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 6 Importorg.junit.Test;7 8 Public classString2date {9 @TestTen Public voidTest ()throwsParseException { OneString string = "2016-10-24 21:59:06"; ASimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); - System.out.println (Sdf.parse (String)); - } the}
Mon OCT 2016 21:59:06 CST
In the case of a string to date operation, it is important to note that the given pattern must match the given string format. Otherwise, the java.text.ParseException exception will be thrown, for example, the following is wrong, the string is not given time and seconds, then SimpleDateFormat of course, can not give you out of thin air to parse the value of seconds and minutes
1 PackageCom.test.dateFormat;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 6 Importorg.junit.Test;7 8 Public classString2date {9 @TestTen Public voidTest ()throwsParseException { OneString string = "2016-10-24"; ASimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); - System.out.println (Sdf.parse (String)); - } the}
However, a given pattern is less than a string and can be
1 PackageCom.test.dateFormat;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 6 Importorg.junit.Test;7 8 Public classString2date {9 @TestTen Public voidTest ()throwsParseException { OneString string = "2016-10-24 21:59:06"; ASimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); - System.out.println (Sdf.parse (String)); - } the}
Mon OCT 2016 00:00:00 CST
It can be seen that the seconds and minutes are 0, not resolved, which is possible.
Conversion between date and string in Java