Conversion between Date and String in Java, javadatestring
When registering a website, we often need to fill in personal information, such as name, age, and date of birth. The value of birth date on the page is a string passed to the background, A date type is required when we store data in the database. In turn, we need to obtain the birth date from the database when it is displayed on the page. This type is of the date type, then you need to convert the date type into a string and display it on the page. The Java API provides a DateForamt class for us to transfer the date and string. DateForamt is an abstract class, so we usually use its subclass SimpleDateFormat. SimpleDateFormat has four constructors, which are most often used as the second one.
In the constructor, pattern is the time mode. The following describes the specific mode in the API:
1. Convert date to string (Format)
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 @ Test10 public void test () {11 Date date = new Date (); 12 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); 13 System. out. println (sdf. format (date); 14 sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 15 System. out. println (sdf. format (date); 16 sdf = new SimpleDateFormat ("MM dd, yyyy HH: mm: ss"); 17 System. out. println (sdf. format (date); 18} 19}
1 2016-10-242 October 24, 2016 21: 59: 063 21:59:06
2. String Conversion date (resolution)
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 {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 24 21:59:06 CST 2016
During string-to-date conversion, note that the given mode must match the given string format; otherwise, java will be thrown. text. parseException exception. For example, the following error occurs. The time and minute are not given in the string. Therefore, SimpleDateFormat cannot parse the time and minute values out of thin air.
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 {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, if the given mode is smaller than the string
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 {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 24 00:00:00 CST 2016
We can see that the time, minute, and second values are all 0 and are not parsed. This is acceptable.