[英]Date format in Database is in yyyy/MM/dd i want it in the from MM/dd/yyyy
您好,在我的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;
}
如果將其存儲為日期,則取決於數據庫。 如果您將其存儲為字符串,則可以使用SimpleDateFormat以所需的格式對其進行格式化,然后將其存儲
年月日是數據庫中日期的標准格式。 日期列不存儲課程的格式,它存儲日期,並且任何格式均由客戶端軟件應用。 某些數據庫客戶端(Oracle)允許定義自定義日期格式,但MySQL不允許。
當客戶端應用程序從數據庫獲取結果時,您始終可以格式化日期。
SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = resultSet.getDate(column);
String dateString = myFormat.format(date);
您還可以使用DATE_FORMAT
函數在數據庫中應用格式化功能。
您不能更改mysql默認日期格式。
您仍然可以選擇使用DATE_FORMAT。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.