我需要将字符串转换为日期以允许我在mysql表中插入信息:
我试过了:
System.out.println("Escribe la fecha de nacimiento de socio:");
//Traduction to english: set the date of birthday //
String fechaSocio = sc.next();
Date fecha = null;
try {
fecha = new SimpleDateFormat("ddMMyyyy").parse(fechaSocio);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但它正在转换:Thu Mar 21 00:00:00 CET 1996
我需要转换为:
1987年1月1日 或01011987
public class SocioDao {
static ClaseConexion conexion = new ClaseConexion();
public void insertar(Socio misocio){
System.out.println(misocio.getDATA_NAIXEMENT());
System.out.println("..........................");
try {
Statement statuo = conexion.getConnection().createStatement();
statuo.executeUpdate("insert into socis(CODI_SOCI,DNI,NOM,COGNOMS,"
+ "DATA_NAIXEMENT,ADRECA,POBLACIO,TELEFON) values ('"
+ misocio.getCODI_SOCI() + "','" + misocio.getDNI()
+ "',' " + misocio.getNOM() + "','" + misocio.getCOGNOMS()
+ "','" + misocio.getDATA_NAIXEMENT() + "','"
+ misocio.getADREÇA() + "','" + misocio.getPOBLACIO()
+ "','" + misocio.getTELEFON() + "')");
statuo.close();
conexion.desconectar();
System.out.println("Socio añadido correctamente");
} catch (Exception e) {
System.out.println("No se puede añadir un nuevo socio");
}
}
}
答案 0 :(得分:4)
答案 1 :(得分:0)
如果您尝试在数据库中插入日期,请使用mysql函数。
例如:
INSERT INTO test (sampledate) VALUES (CURDATE());