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 when we deposit it into the database, we do need a date type. Conversely, when it is displayed on the page, we need to obtain the date of birth from the database. At this time, this type is a date type, and then the date type needs to be converted into a string to display on the page. Java API provides us with a class DateForamt that transfers the date and string to each other. DateForamt is an abstract class, so it is usually used as its subclass SimpleDateFormat. SimpleDateFormat has 4 constructors, the second one is most often used.
The pattern in the constructor is the time pattern. What are the specific patterns? The API is explained as follows
1. Date to string (formatted)
package com.test.dateFormat;import java.text.SimpleDateFormat;import java.util.Date;import org.junit.Test;public class Date2String { @Test 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("yyyyy year MM month dd date HH:mm:ss"); System.out.println(sdf.format(date)); }} 2016-10-24
2016-10-24 21:59:06
October 24, 2016 21:59:06
2. String to date (parsement)
package com.test.dateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import org.junit.Test;public class String2Date { @Test public void test() throws ParseException { String string = "2016-10-24 21:59:06"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.parse(string)); }} Mon Oct 24 21:59:06 CST 2016
When using string to date, you need to note that the given pattern must match the given string format, otherwise a java.text.ParseException will be thrown. For example, the following is wrong. The string does not give time, minute and second. Of course, SimpleDateFormat cannot parse the time, minute and second values out of thin air.
package com.test.dateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import org.junit.Test;public class String2Date { @Test public void test() throws ParseException { String string = "2016-10-24"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.parse(string)); }}However, a given pattern is less than a string
package com.test.dateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import org.junit.Test;public class String2Date { @Test public void test() throws ParseException { String string = "2016-10-24 21:59:06"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.parse(string)); }} 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 OK.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.