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 package Com.test.dateFormat; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.junit.Test; 7 8 public class Da te2string {9 @Test10 public Void Test () {One date date = new Date (); simpledateformat sdf = new Simpleda Teformat ("Yyyy-mm-dd"), System.out.println (Sdf.format (date)), SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss "), System.out.println (Sdf.format (date)), and SDF = new SimpleDateFormat (" YYYY year mm month DD Day HH:mm:ss "); System.out.println (Sdf.format (date)); }19}
1 2016-10-242 2016-10-24 21:59:063 October 24, 2016 21:59:06
2. String to date (parsing)
1 package Com.test.dateFormat; 2 3 import java.text.ParseException; 4 import Java.text.SimpleDateFormat; 5 6 import org.junit.Test; 7 8 public class String2date {9 @Test10 public Void Test () throws ParseException {One string string = "2016-10-24 21:59:06 "; simpledateformat sdf = new SimpleDateFormat (" Yyyy-mm-dd HH:mm:ss "), System.out.println ( Sdf.parse (String)); }15}
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 package Com.test.dateFormat; 2 3 import java.text.ParseException; 4 import Java.text.SimpleDateFormat; 5 6 import org.junit.Test; 7 8 public class String2date {9 @Test10 public Void Test () throws ParseException {One string string = "2016-10-24" , simpledateformat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"), System.out.println ( sdf.parse ( string)); }15}
However, a given pattern is less than a string and can be
1 package Com.test.dateFormat; 2 3 import java.text.ParseException; 4 import Java.text.SimpleDateFormat; 5 6 import org.junit.Test; 7 8 public class String2date {9 @Test10 public Void Test () throws ParseException {One string string = "2016-10- SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd"), System.out.println (Sdf.parse 21:59:06) (string)); }15}
Mon OCT 2016 00:00:00 CST
It can be seen that the seconds and minutes are 0, not resolved, which is possible.
Reprinted from:https://www.cnblogs.com/huangminwen/p/5994846.html
Conversion between date and string in Java