gpt4 book ai didi

java - 日期格式 JSON jqgrid

转载 作者:行者123 更新时间:2023-11-29 18:08:55 25 4
gpt4 key购买 nike

我正在用 java 开发一个网络应用程序。我正在用 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;
}

最佳答案

您可以添加一个格式化程序来根据您所需的格式格式化日期值:

{
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

关于java - 日期格式 JSON jqgrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47565155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com