Use SimpleDateFormat Note and convert string to date in JSP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.