When we register the website, we often need to fill in personal information, such as name, age, date of birth, and so on, when 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 when we put it in the database, in turn, we need to get the date of birth from the database when it is displayed The type is a date type, and then the date type needs to be turned into a string to be displayed on the page, and the Java API provides us with a class Dateforamt for the date and string to be transported to each other. Dateforamt is an abstract class, so it usually uses its subclass SimpleDateFormat. SimpleDateFormat has 4 constructors, most often the second.
The pattern in the constructor is the time pattern, and the specific patterns are described in the API, as follows
1. Date-turn 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 Date2string {
9 @Test
Ten public void Test () {
Date date = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
System.out.println (Sdf.format (date));
SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
System.out.println (Sdf.format (date));
SDF = new SimpleDateFormat ("YYYY year mm month DD Day HH:mm:ss");
System.out.println (Sdf.format (date));
18}
19}
1 2016-10-24
2 2016-10-24 21:59:06
3 October 24, 2016 21:59:06
2, String date (parsing)
1 Package Com.test.dateFormat;
2
3 import java.text.ParseException
4 import java.text.SimpleDateFormat;
5 br> 6 import org.junit.Test;
7
8 public class String2date {
9 @Test
10 & nbsp public void Test () throws ParseException {
11 string string = ' 2016-10-24 21:59:06 ";
12 SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-MM-dd HH:mm : ss ");
13 System.out.println (Sdf.parse (string));
14 }
15}
Mon Oct 21:59:06 CST 2016
in a string-date operation, it is important to note that the given pattern must match the given string format. Otherwise, the java.text.ParseException exception is thrown, for example, the following is wrong, the string does not give a time and seconds, then SimpleDateFormat of course can not give you the time to parse out the value of the seconds to
1 Package Com.test.dateFormat;
2
3 import java.text.ParseException
4 import java.text.SimpleDateFormat;
5 br> 6 import org.junit.Test;
7
8 public class String2date {
9 @Test
10 & nbsp public void Test () throws ParseException {
11 string string = ' 2016-10-24 ";
12 SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-MM-dd HH:mm : ss ");
13 System.out.println (Sdf.parse (string));
14 }
15}
However, a given pattern is less than a string can be
1 Package Com.test.dateFormat;
2
3 import java.text.ParseException
4 import java.text.SimpleDateFormat;
5 br> 6 import org.junit.Test;
7
8 public class String2date {
9 @Test
10 & nbsp public void Test () throws ParseException {
11 string string = ' 2016-10-24 21:59:06 ";
12 SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-MM-dd");
13 System.out.println (Sdf.parse (string));
14 }
15}
Mon Oct 00:00:00 CST 2016
can be seen as a minute and a 0, not resolved.