SimpleDateFormatt write with empty field

0

I'm having trouble writing an empty field because it's being formatted with SimpleDateFormatt ..

Writing 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 anyone help me with how to make it write even if the field is empty?

In the database there is no null ...

    
asked by anonymous 14.11.2016 / 04:02

1 answer

0

Your problem is not time saved in the bank.

The exception java.text.ParseException: Unparseable date: "" refers to the parse you try to make of an empty%%.

It is very simple to solve 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));
      }

You can now save your ServiceObject object normally.

    
30.11.2016 / 13:57