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.