Springmvc the date method for receiving the string and the return string type date to the front end

Source: Internet
Author: User
Tags string format

Date type data sent over the front end cannot receive


	<mvc:annotation-driven conversion-service= "Conversionservice"/>   <!--The original wording injected the time parameter converter parameter string Convert to date type  -->
	
	<bean id= "Conversionservice" class= " Org.springframework.format.support.FormattingConversionServiceFactoryBean "> 
		<property name=" Converters ">
			<list>
				<bean class=" Com.bim.revit.controller.common.DateConverter "/>
			</list>
		</property>
	</bean>


And then I found a single date matching method.

@DateTimeFormat (pattern= "Yyyy-mm-dd HH:mm:ss")
It's okay.

@JsonFormat (pattern= "Yyyy-mm-dd HH:mm:ss", timezone = "gmt+8")

Returns the time of the front-end string format


Reference

  When querying the database, the date type returned is Java.sql.date, The browser is set to Java.util.date in a custom node object, but the sql.date loses information about the time, only the date, the SQL is queried as a string, and the resulting string is converted to Util.date, when the browser displays a long timestamp;
    Workaround: Database query result is string, converted to Util.date added to node object, node object's definition of date type variable, add annotation:
@DateTimeFormat (pattern= " Yyyy-mm-dd HH:mm:ss ")
@JsonFormat (pattern=" Yyyy-mm-dd HH:mm:ss ", timezone =" gmt+8 ")
    Spring formats the date type variables according to the Jsonfromat format, and the browser get requests the node object to display the time normally.
    Add the annotation
    @DateTimeFormat (pattern= "Yyyy-mm-dd HH:mm:ss") to the definition of the object
    @JsonFormat (pattern= "Yyyy-mm-dd HH:mm:ss"). TimeZone = "Gmt+8")
    private Date crtdate;
    @DateTimeFormat (pattern= "Yyyy-mm-dd HH:mm:ss")
    @JsonFormat (pattern= "Yyyy-mm-dd HH:mm:ss", timezone = "gmt+8")
    private Date upddate;
1 2 3 4 5 6 7
    SQL query Statement
     sql=string.format ("Select Id,uuid,upddate,crtdate from TRAN0401");
        try {
            Boolean result=st.execute (SQL);
            if (result)
            {
                rs=st.getresultset ();
                Jxnode node;
                while (Rs.next ())
                {                
                    node=new jxnode ();
                    String crtdate=rs.getstring ("crtdate"); Must be taken out as a string, or it will lose data
                    string upddate=rs.getstring ("Upddate");
                    Node.setcrtdate (Formatter.parse (crtdate));
                    Node.setupddate (Formatter.parse (upddate));
                }
            


SPRINGMVC returns JSON date format, @DatetimeFormat use note when using @responsebody

SPRINGMVC date format that returns JSON when using @responsebody

Prerequisite Knowledge: @ResponseBody The core class that returns the JSON string is Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter, which uses the open source Third-party class library of Jackson. This is mainly the following two jar packages: Jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar.

Problem: Returns the date format of the JSON string when using @responsebody. The Date Type property returns a long timestamp by default, and how it can return a custom date format.

Solution: There are two ways to achieve this,

1, local modification (online more, but not recommended);

Inherit Jackson's abstract class: Jsonserializer<t>, and then add the annotation @jsonserialize on JavaBean's property getter ().

The code is as follows:

  Java code    import java.io.ioexception;   import java.text.simpledateformat;    import java.util.date;      import org.codehaus.jackson.jsongenerator;    import org.codehaus.jackson.jsonprocessingexception;   import  org.codehaus.jackson.map.jsonserializer;   import org.codehaus.jackson.map.serializerprovider;      /**   *  @description   Custom return json  date format processing   in the data grid  * @ author aokunsang   *  @date  2013-5-28   */   public class  customdateserializer extends jsonserializer<date> {            @Override        public void serialize (Date value,                 JsonGenerator  jsongenerator,                SerializerProvider  Provider)                throws  ioexception, jsonprocessingexception {            Simpledateformat sdf = new simpledateformat ("Yyyy-mm-dd hh:mm:ss");            jsongenerator.writestring (Sdf.format (value));        }  }  

How to use:

Java code

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.