MySQL日期一天拆分

时间:2019-03-07 00:10:52

标签: java mysql

我正在尝试做一个非常简单的Java Web应用程序,该应用程序可以获取一个人的信息:姓氏,姓氏,出生日期,并将它们存储在MySql数据库中。 问题是当我尝试在数据库中插入日期(例如07/03/2019)时,日期是前一天(06/03/2019)。 如何解决这个问题? 代码下方:

package bean;

import java.io.Serializable;
import java.sql.Date;

public class Persona implements Serializable {

private int idPersona;
private String nome;
private String cognome;
private Date dataNascita;

public Persona() {
    super();

}

public Persona(String nome, String cognome, Date dataNascita) {
    super();
    this.nome = nome;
    this.cognome = cognome;
    this.dataNascita = dataNascita;
}

public Persona(int idPersona, String nome, String cognome, Date dataNascita) 
{
    super();
    this.idPersona = idPersona;
    this.nome = nome;
    this.cognome = cognome;
    this.dataNascita = dataNascita;
}

public int getIdPersona() {
    return idPersona;
}

public void setIdPersona(int idPersona) {
    this.idPersona = idPersona;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCognome() {
    return cognome;
}

public void setCognome(String cognome) {
    this.cognome = cognome;
}

public Date getDataNascita() {
    return dataNascita;
}

public void setDataNascita(Date dataNascita) {
    this.dataNascita = dataNascita;
}

}

servlet

package servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Persona;
import dao.ProvaDao;
import utility.ManipolazioneDate;


@WebServlet("/Inserimento")
public class Inserimento extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public Inserimento() {


}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {

    String nome = request.getParameter("nome");
    String cognome = request.getParameter("cognome");
    String dataNascita = request.getParameter("dataNascita");
    Date dataN = null;
    try {
        dataN = (Date) ManipolazioneDate.convertiData(dataNascita);
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Persona p = new Persona(nome, cognome, dataN);
    System.out.println(p.getNome());
    System.out.println(p.getCognome());
    System.out.println(p.getDataNascita());

    try{
        ProvaDao.registraPersona(p);
    }catch(SQLException e){
        e.printStackTrace();
    }
}

} 使用静态方法将String日期格式解析为java.sql.Date格式的类

package utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ManipolazioneDate {

public static Date convertiData(String data) throws ParseException{

    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    Date parser = format.parse(data);
    java.sql.Date dataSql = new java.sql.Date(parser.getTime());

    return dataSql;

}

}

Dao

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import bean.Persona;
import utility.Connessione;

public class ProvaDao {

    public static void registraPersona(Persona persona) throws SQLException{

        Connection conn = Connessione.getConnection();
        PreparedStatement ps = null;


        String ins = "insert into prova2.persona(nome, cognome, dataNascita) 
values(?,?,?)";

        try{

            ps = conn.prepareStatement(ins);

            ps.setString(1, persona.getNome());
            ps.setString(2, persona.getCognome());
            ps.setDate(3, persona.getDataNascita());

            ps.executeUpdate();
            System.out.println("Persona inserita");

        }catch(SQLException e){

            System.out.println(e.getMessage());
            System.out.println("Errore nell'inserimento");

        }finally{

            if(ps != null){
                ps.close();
            }

            if(conn != null){
                conn.close();
            }
        }
    }

}

JSP表单

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<div align="center">
		<form action="/Prova/Inserimento" method="get">
			<p>Nome</p>
			<input type="text" value="" name="nome">
			<br>
			<p>Cognome</p>
			<input type="text" value"" name="cognome">
			<br>
			<p>Data di nascita</p>
			<input type="text" value="" name="dataNascita">
			<br>
			<input type="submit" value="Invia">
			
		</form>
	</div>
</body>
</html>

谢谢。

1 个答案:

答案 0 :(得分:0)

也许代码没有错。 (因为代码无法操纵日期时间。)

在Java代码中,进行SELECT CURRENT_TIMESTAMP;的获取并检查时间戳。如果时间戳记错误,则归因于MySQL服务器。