One, the use of SimpleDateFormat Note place
SimpleDateFormat Convert a string to a date type, the format of the conversion must be the same as the format of the string, or it will be an error, such as: Convert the string "20150825195057" to a date type, the conversion format must be
"Yyyymmddhhmmss", in other formats, will be reported java.text.ParseException:Unparseable date: "20150825195057" exception. What if you want to convert the string to a YYYY-MM-DD HH:mm:ss format? Workaround: First convert the string to its own date format, and then convert the converted date to another format. The code is as follows
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classTest { Public Static voidMain (string[] args) {String parsedate= "20150825195057"; SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyymmddhhmmss"); SimpleDateFormat SDF2=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Try{Date Date=Sdf.parse (parsedate); System.out.println (Sdf2.format (date)); //output Results 2015-08-25 19:50:57}Catch(ParseException e) {e.printstacktrace (); } }}
Ii. converting a string into a date type in a JSP
This idea is the same as in the direct conversion in Java, first convert the string in its original format into a date format, and then convert the date to the format you want.
The conversion should first introduce two header files
<%@taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C"%><%@taglib URI="http://java.sun.com/jsp/jstl/fmt"prefix="FMT" %>
Then convert the date
< fmt:parsedate value = "20150825195057" pattern = "Yyyymmddhhmmss" var = "Date" ></ fmt:parsedate > < fmt:formatdate value = "${date}" pattern = "Yyyy-mm-dd" />//Output 2015-08-25
where Var in <fmt:parsedate> is passed to value in <fmt:formatdate>.
Use SimpleDateFormat Note and convert string to date in JSP