The database date time is displayed on the page in a malformed solution

Source: Internet
Author: User

In the process of doing the project will certainly encounter a problem: the database is stored in a date or datetime type value, from the database to be taken out of the page to display the long type or the GTM type DateTime.
For this problem, after the study has the following conclusions:

1.hibernate does not have this problem, and Mabatis will have this problem.

2. The database uses the date type to display a long type datetime on the page, and the GTM type date time is displayed on the page using the datetime type.

Because I use the MySQL database, the other database has not been tested, so I do not know what the other database is, here is the case for the Mybatis+mysql database for my solution.

General Solution : Change the parameters in an entity class from the date type to a string type, one cure, but if you do not want to solve the child shoes in this way, please look down ~

Solution with GTM time (green time):

Scenario One (custom, from the background to solve the problem): in the background to encapsulate the data into a map, key is the formatted time string, and then in the foreground to traverse the map, the value is {Map.key} on the line. PS: Customized solution, for reference only ~

Scenario two (recommended, from the front-end to solve the problem): I did not think of Jstl tag library can also format the processing of data this stubble, so later still use this kind of scheme. In the front-end to solve the matter is not put in the background to solve, reduce the principle of server pressure, I decisively chose this method ~ (in fact, this way simple, want to lazy just ...) )

Common Web Project Introduction jar package, MAVEN Project Add Jstl dependency, add dependency can be referenced: http://www.cnblogs.com/ggqzz/p/8421734.html

Introduce tags:

<%@ taglib prefix= "FMT" uri= "Http://java.sun.com/jsp/jstl/fmt"%>

Add code where the date is traversed

<fmt:formatdate type= "Both" value= "${order.ordertime}"/>

A long type of time solution appears:

Scenario One: Jstl tag library FMT tag, as mentioned above, will not say much.

Because this project was done with Easyui, and the data was traversed by the DataGrid and did not use the EL expression, I used the following 2 ways.

Scenario two: Using the DataGrid's own formatter (formatted column) property, you can define the function directly, and then add formatter="function name" (Note that the following does not need parentheses) on the column that needs to be formatted to OK.

Custom functions, which is actually a way of JS formatting time:

function FormatDate (val) {            var date = new date (val);            var y = date.getfullyear ();            var m = date.getmonth () + 1;            var d = date.getdate ();            Return y + '-' + M + '-' + D;}     

Way three (recommended): This way is to use the notes provided by Jackson, personal feeling is still very useful ~

Introduce dependencies First:

<!--jackson-databind Dependent-        <dependency>            <groupid>com.fasterxml.jackson.core</ groupid>            <artifactId>jackson-databind</artifactId>            <version>2.9.2</version >        </dependency>

Add annotations to the entity class date type attribute:

@JsonFormat (pattern= "Yyyy-mm-dd", timezone= "Gmt+8")    private Date opendate;

Note: timezone= "gmt+8" This attribute must be added, otherwise the time zone is wrong will cause the date deviation! This way also formats the GTM time!

In the future if there are more solutions to this problem I will continue to update, welcome message Discussion ~

Database datetime Displays a malformed solution on the page

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.