Solve the problem of java string conversion to time Unparseable date error

  • 2021-09-20 20:35:49
  • OfStack

Today, when writing code, when converting strings into time, there is such a problem.

java.text.ParseException: Unparseable date: "2017-1-1 00:00:00"


DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date start = df.parse(startTime);
        Date stop = df.parse(stopTime);
        long timeSpan = stop.getTime() - start.getTime();
        long hours = timeSpan / (3600 * 1000);

I use the above code to calculate the time difference of the incoming time, but the above problem appears when running. Baidu has a time conversion, and the length of the string should be 11.

That is to say, it should be 1 length with this "yyyy-MM-dd". For example, if 2013-1-1 is to be converted into time, it needs to be written as "2013-01-01";

Then the conversion can be successful!

java. text. ParseException: ES22date: ""-Exception 6

Scenario:

In the time period query of struts2 project, the following java. text. ParseException: Unparseable date was reported at action: "", there is no problem in checking the code under 1, and judgment is made when receiving data, but the desired result is not obtained, and some are depressed. After solving it, record 1 here:

[1] Previous error code:


 if (bgndate!=null&&bgndate!=""&&enddate!=null && enddate!="")) 

It appears here. When the request time is empty, I shouldn't have left, but I actually left. It must be that there is something wrong with my judgment


    public String yaoqinglist() throws Exception {
        try{
             //bgndate  And  enddate  Passed in is the type of string 
            ActWork actWork=new ActWork();
            //  Lowercase mm Represents minutes 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (bgndate!=null&&bgndate!=""&&enddate!=null && enddate!="")) {
 
                Date bDate = sdf.parse(bgndate);
                Date eDate = sdf.parse(enddate);
                String format = sdf.format(bDate);
                String startTime  = format.replace("-", "");//  De-special character 
                actWork.setStartTime(startTime);// Start time of exhibition   Such as :20190401
                String format1 = sdf.format(eDate);//  De-special character 
                String endTime = format1.replace("-", "");
                actWork.setEndTime(endTime);// End time of exhibition   Such as :20190430
            }
            .... Omission 
 
          }catch (Exception e){
            log.error(e.toString(), e);
            return ERROR;
        }
        return "worklist";

[2] Look for reasons:

The use of java data type is wrong, awkward, java foundation is very important! As we all know, java is divided into basic data types (byte, short, char, int, long, float, double, boolean) and composite types (String, Intger, Long, etc.). The basic types are compared with "= =", and the composite types are compared with "equals", while the variables I use are String types, but I use bgndate! = "" This will definitely not work, just use equals.

[3]


if (bgndate!=null&&!bgndate.equals("") &&enddate!=null&& !enddate.equals("")) 

The solution is as follows:


    public String yaoqinglist() throws Exception {
        try{
             //bgndate  And  enddate  Passed in is the type of string 
            ActWork actWork=new ActWork();
            //  Lowercase mm Represents minutes 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (bgndate!=null&&!bgndate.equals("") &&enddate!=null&& !enddate.equals("")) {
 
                Date bDate = sdf.parse(bgndate);
                Date eDate = sdf.parse(enddate);
                String format = sdf.format(bDate);
                String startTime  = format.replace("-", "");//  De-special character 
                actWork.setStartTime(startTime);// Start time of exhibition   Such as :20190401
         
                String format1 = sdf.format(eDate);//  De-special character 
                String endTime = format1.replace("-", "");
                actWork.setEndTime(endTime);// End time of exhibition   Such as :20190430
            }
            .... Omission 
 
          }catch (Exception e){
            log.error(e.toString(), e);
            return ERROR;
        }
        return "worklist";

That's it for OK! (^ _ ^)


Related articles: