Simpledateformatt write with empty field

Asked

Viewed 28 times

0

I’m having trouble recording an empty field as it is being formatted with Simpledateformatt..

Saving the console generates an error

    java.text.ParseException: Unparseable date: ""

Servlet

    private void gravarProximo(HttpServletRequest request, HttpServletResponse response) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy HH:mm");


        OrdemServico c = (OrdemServico) request.getSession().getAttribute("ordemServico"); 

                c.setDataOpen(sdf.parse(request.getParameter("dataOpen")));
                c.setDataClosed(sdf2.parse(request.getParameter("dataClosed")));

Can someone help me on how to make it record even if the field is empty?

in the database is without null...

1 answer

0

Your problem is not time to save in the bank.

A Exception java.text.ParseException: Unparseable date: "" concerns the parse you try to make of a String empty.

It is very simple to resolve this, just check first if the string is empty:

private void gravarProximo(HttpServletRequest request, HttpServletResponse response) {
  try {
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
      SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
      OrdemServico c = (OrdemServico) request.getSession().getAttribute("ordemServico"); 

      String dataOpen = request.getParameter("dataOpen");
      if(dataOpen != null && !dataOpen.isEmpty()){
           c.setDataOpen(sdf.parse(dataOpen));
      }
      String dataClosed = request.getParameter("dataClosed");
      if(dataClosed != null && dataClosed.isEmpty()){
           c.setDataClosed(sdf2.parse(dataClosed));
      }

Now you can save your object Ordemservic normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.