Processing of time format issues for JsonView generation in etmvc

Source: Internet
Author: User

I used ActiveRecord in etmvc to implement the orm. When the JsonView is generated and returned, I found that the date and time formats have all changed to date and no time. Why?

The Code is as follows:
[Java]
Public JsonView getLogs (int rows, int page
) Throws Exception {
String cond = "1 = 1 ";
List <Object> tmpArgs = new ArrayList <Object> ();
Object [] args = tmpArgs. toArray ();

Long total = Log. count (Log. class, cond, args); // The total number of queries.
List <Log> logs = Log. findAll (Log. class, cond, args, "id", rows, (page-1) * rows); // query one page of data

// Construct the JSON data structure and return the JSON View
Map <String, Object> result = new HashMap <String, Object> ();
Result. put ("total", total );
Result. put ("rows", logs );
// System. out. println (result. toString ());
JsonView view = new JsonView (result );
View. setContentType ("text/html; charset = UTF-8 ");
Return view;
}
The result is returned as follows. The time is gone, and only the date is returned:

 

I did not talk about this on the internet for a long time. I guess later, it may be that by default, JsonView will divide the time by year, month, day, hour, and minute, the second attribute is displayed, but it is not the complete time string we want. What should we do? Later, I thought about how to manually format the string. The Code is as follows:

[Java]
Public JsonView getLogs (int rows, int page
) Throws Exception {
String cond = "1 = 1 ";
List <Object> tmpArgs = new ArrayList <Object> ();
Object [] args = tmpArgs. toArray ();

Long total = Log. count (Log. class, cond, args); // The total number of queries.
List <Log> logs = Log. findAll (Log. class, cond, args, "id", rows, (page-1) * rows); // query one page of data

List <Map <String, Object >>_list = new ArrayList <Map <String, Object> ();
For (Log log: logs ){
Map <String, Object> _ map = new HashMap <String, Object> ();
_ Map. put ("id", log. getId ());
_ Map. put ("userName", log. getUserName ());
_ Map. put ("userIP", log. getUserIP ());
_ Map. put ("logTime", log. getLogTime (). toString ());
_ List. add (_ map );
}

// Construct the JSON data structure and return the JSON View
Map <String, Object> result = new HashMap <String, Object> ();
Result. put ("total", total );
Result. put ("rows", _ list );
// System. out. println (result. toString ());
JsonView view = new JsonView (result );
View. setContentType ("text/html; charset = UTF-8 ");
Return view;
}
Result returned:

 

The problem has been solved for now. I don't know if there are other methods. I haven't studied it yet! The database is MySQL, And the datetime format is datetime. The model uses the java. SQL. Timestamp format.

Related Article

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.