Date type to string and string to date type

Source: Internet
Author: User
Tags dateformat

The date type is a string and a string date type, which is converted manually in Jsp/servlet and is automatically converted in struts2

SimpleDateFormat is a specific class that formats and resolves dates in a locale-related manner. It allows formatting (date-> text), parsing (text-> date), and normalization

Take a look at the converted code:

Package com.ttgbook.conver;

Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import java.util.Date;

public class Conver {
	
	//convert date to string public
	static string convertostring (date date)
	{
		DateFormat df = new Sim Pledateformat ("Yyyy-mm-dd");
		
		return Df.format (date);
	Convert string to date public
	static Date convertodate (String strdate) throws Exception
	{
		DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
		Return Df.parse (strdate);
	}
     

When a string is converted to a date, an exception is thrown

DateFormat is an abstract class





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.