Hello在我的Web应用程序中,我使用SimpleDateFormat将字符串转换为日期,格式为MM / dd / yyyy但是当表单字段在数据库中插入时,格式为yyyy / MM / dd。
下面是我的servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Affiliate af= new Affiliate();
af.setFisrtName(request.getParameter("txtFname"));
af.setLastName(request.getParameter("txtLname"));
af.setGender(request.getParameter("txtGender"));
af.setCategory(request.getParameter("txtCategory"));
String dob=(request.getParameter("txtDob"));
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date;
try {
date = (Date)formatter.parse(dob);
af.setDate(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
af.setAge(Integer.parseInt(request.getParameter("txtAge")));
af.setAddress(request.getParameter("txtAddr"));
af.setCountry("India");
af.setState(request.getParameter("txtState"));
af.setCity(request.getParameter("txtCity"));
af.setPinCode(Integer.parseInt(request.getParameter("txtPin")));
af.setEmailId(request.getParameter("txtEmail"));
af.setStd(Integer.parseInt(request.getParameter("txtStd")));
af.setContactNo(Integer.parseInt(request.getParameter("txtPhone")));
af.setMobileNo(Long.parseLong(request.getParameter("txtMobile"),10));
AffiliateService afs=new AffiliateService();
**afs.createAffiliate(af);**
}
以下是我的DAO:
public void insertAffiliate(Affiliate affiliate){
String sql="INSERT INTO REGISTER " +"(id,FisrtName,LastName,Gender,Category,DateOfBirth,Age,Address,Country,State,City,PinCode,EmailId,Std,ContactNo,MobileNo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Connection conn = null;
try {
**conn = dataSource.createConnection();**
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, affiliate.getId());
ps.setString(2, affiliate.getFisrtName());
ps.setString(3, affiliate.getLastName());
ps.setString(4,affiliate.getGender());
ps.setString(5, affiliate.getCategory());
ps.setDate(6, new java.sql.Date(affiliate.getDate().getTime()));
ps.setInt(7, affiliate.getAge());
ps.setString(8, affiliate.getAddress());
ps.setString(9,affiliate.getCountry());
ps.setString(10,affiliate.getState());
ps.setString(11, affiliate.getCity());
ps.setInt(12, affiliate.getPinCode());
ps.setString(13, affiliate.getEmailId());
ps.setInt(14,affiliate.getStd());
ps.setInt(15, affiliate.getContactNo());
ps.setLong(16, affiliate.getMobileNo());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
以下是我的DTO:
public class Affiliate {
@NotNull
@Past
Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
答案 0 :(得分:3)
如果您将其存储为日期,则其数据库依赖于。如果你将它存储为String,那么你可以使用你想要的格式使用SimpleDateFormat格式化它,然后存储它
答案 1 :(得分:0)
年 - 月 - 日是数据库中日期的标准格式。日期列当前不存储格式,它存储日期,客户端软件应用任何格式。某些数据库客户端(Oracle)允许定义自定义日期格式,但不允许定义MySQL。
当从数据库中获取结果时,您始终可以在客户端应用程序中格式化日期。
SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = resultSet.getDate(column);
String dateString = myFormat.format(date);
您还可以使用DATE_FORMAT
功能在数据库中应用格式化功能。
答案 2 :(得分:0)
您无法更改mysql默认日期格式。
您仍然可以在选择中使用DATE_FORMAT。