2
I have the following data insertion interface:
The comments below represent attempts to fix the error but none worked.
In HTML the type
is datetime-local
, in the bank the type was created as datetime and in the class ControladoraJPA
created by java directly connected with the table in the database, the attribute came only as Date
.
I have the following java method:
public void cadastrarPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
try{
String conteudo = request.getParameter("content_post");
String data_cria = request.getParameter("data_criacao");
String data_public = request.getParameter("data_publicacao");
String categoria = request.getParameter("categoria");
//data_cria = data_cria.replace('T', ' ').concat(":00");;
//data_public = data_cria.replace('T', ' ').concat(":00");
//DateTimeFormatter formato = DateTimeFormatter.forPattern("dd/MM/yyyy HH:mm:ss");
//Date date = (Date) formato.parse(data_cria);
//Date date2 = (Date) formato.parse(data_public);
//java.sql.Timestamp data1 = java.sql.Timestamp.valueOf(data_cria);
//java.sql.Timestamp data2 = java.sql.Timestamp.valueOf(data_public);
//DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
//Date date = (Date)formatter.parse(data_cria);
//Date date2 = (Date)formatter.parse(data_public);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PROJETO_POSTPU");
UserJpaController userJpa = new UserJpaController(emf);
PostJpaController postJpa = new PostJpaController(emf);
CategoryJpaController categoryJpa = new CategoryJpaController(emf);
List<Category> listCat = new ArrayList();
listCat = categoryJpa.findCategoryEntities();
List<User> listUser = new ArrayList();
listUser = userJpa.findUserEntities();
Post post = new Post();
post.setContent(conteudo);
post.setCreatedAt(date);
post.setPublishedAt(date2);
for(Category c : listCat){
if(c.getDescription().equals(categoria)){
post.setIdCategory(c);
break;
} else {
continue;
}
}
for(User u : listUser){
if(u.getUsername() == userSec){
post.setIdAuthor(u);
}
}
postJpa.create(post);
//request.setAttribute("resp", result);
request.getRequestDispatcher("/feed/feed.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
Wouldn’t it be something like
DateTimeFormatter.forPattern("yyyy-MM-dd\THH:mm")
?– Woss
@Andersoncarloswoss It actually is
yyyy-MM-dd'T'HH:mm
(oruuuu-MM-dd'T'HH:mm
). Although, for the ISO 8601 format, you don’t even need aDateTimeFormatter
(if you’re going to use java.time, of course, because forDate
andSimpleDateFormat
need), see my answer down below– hkotsubo