日期格式 JSON jqgrid
Posted
技术标签:
【中文标题】日期格式 JSON jqgrid【英文标题】:Date format JSON jqgrid 【发布时间】:2018-05-13 21:24:58 【问题描述】:我正在用 Java 开发一个 Web 应用程序。我正在用 JSON 数据填充 jqgrid。问题是“生日”列显示的格式不正确。在 mysql 数据库中,该字段为 DATE
类型,但在 jqgrid 中显示为:“Dec 30, 1989”。我需要以下格式:'1989-12-30'
我的 servlet 代码:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
String parameter = request.getParameter("parameter");
String username = request.getParameter("username");
int option = Integer.parseInt(request.getParameter("option"));
DBConnection db = new DBConnection();
StringBuilder sb = new StringBuilder();
Connection con = (Connection) db.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Customers> customerList = new ArrayList<Customers>();
try
sb.append("call SP_MSTCUSTOMERS_FIND(?,?,?)");
ps = con.prepareCall(sb.toString());
ps.setString(1, parameter);
ps.setString(2, username);
ps.setInt(3, option);
rs = ps.executeQuery();
while (rs.next())
Customers customer = new Customers();
customer.setIdMstCustomers(rs.getInt("IDMSTCUSTOMERS"));
customer.setFirstName(rs.getString("FIRST_NAME"));
customer.setLastName(rs.getString("LAST_NAME"));
customer.setBirthdate(rs.getDate("BIRTHDATE"));
customerList.add(customer);
catch (SQLException e)
e.printStackTrace();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customers>>()
.getType());
JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);
jqgrid的代码:
$("#list").jqGrid(
url: 'PopulateCustomersTable',
postData:
parameter: function ()
return $("#parameter").val();
,
username: function ()
return $("#txtusrID").val();
,
option: function ()
return $("input[name='srch']:checked").val(); //$('[name="srch"]').val();
,
datatype: "json",
mtype: 'POST',
colNames: ['Id', 'First Name', 'Last Name', 'Birthdate'],
colModel: [
name: 'IdMstCustomers',
index: 'IdMstCustomers',
width: 50,
fixed: true,
align: 'center'
,
name: 'FirstName',
index: 'FirstName',
width: 100,
fixed: true,
align: 'center',
editable: true
,
name: 'LastName',
index: 'LastName',
width: 100,
fixed: true,
align: 'center',
editable: true
,
name: 'Birthdate',
index: 'Birthdate',
width: 100,
editable: true,
],
pager: '#pager',
rowNum: 10,
rowList: [10, 20, 30],
sortname: 'FirstName',
sortorder: 'desc',
viewrecords: true,
gridview: true,
caption: 'Customers',
jsonReader:
repeatitems: false
,
editurl: "CustomersServlet"
);
$('#list').trigger('reloadGrid');
$('#list').jqGrid('setGridWidth', '900');
jQuery("#list").jqGrid('navGrid', '#pager',
edit: true,
add: false,
del: false,
search: true
);
The jqgrid data looks like this
我的客户类
public Customers(int IdMstCustomers, String FirstName, String LastName, Date Birthdate)
this.setIdMstCustomers(IdMstCustomers);
this.setFirstName(FirstName);
this.setLastName(LastName);
this.setBirthdate(Birthdate);
public Customers()
private int IdMstCustomers;
private String FirstName;
private String LastName;
private Date Birthdate;
public void setIdMstCustomers(int IdMstCustomers)
this.IdMstCustomers = IdMstCustomers;
public void setFirstName(String FirstName)
this.FirstName = FirstName;
public void setLastName(String LastName)
this.LastName = LastName;
public void setBirthdate(Date Birthdate)
this.Birthdate = Birthdate;
public int getIdMstCustomers()
return IdMstCustomers;
public String getFirstName()
return FirstName;
public String getLastName()
return LastName;
public Date getBirthdate()
return Birthdate;
【问题讨论】:
请发布您的源 JSON 数据,而不是 jqGrid 中显示的数据。还有一些其他注意事项:绝对不需要运行命令$('#list').trigger('reloadGrid');
- 使用此命令,您在构建网格时再次重新加载网格,但什么也没做
【参考方案1】:
您可以添加formatter
以根据您所需的格式格式化日期值:
name: 'Birthdate',
index: 'Birthdate',
width: 100,
editable: true,
formatter: 'date', formatoptions: srcformat: 'M, d, Y', newformat: 'Y-m-d'
您可以在此处阅读更多信息:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_formatter
【讨论】:
对我不起作用,仍然显示不正确的格式:70707070-JanJan-0101 谢谢@Waqas!为我做了一个非常小的调整:formatoptions: srcformat: 'M d, Y', newformat: 'Y-m-d' M of month 部分后面的逗号是问题 很好,那您能把它标记为正确答案吗?谢谢以上是关于日期格式 JSON jqgrid的主要内容,如果未能解决你的问题,请参考以下文章