String Date type time conversion to the time

Database to retrieve data according to the time period, date and time of use to layui.laydate selection component, as follows:

layui.use(['layer','laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#endTime'
            , type: 'datetime'
        });
    });

Setting time can not have [range: true], otherwise it will cause each input there are two time select components;

Select the type of type datetime, because we need to select the year, month, day, hour, minute, second, date type can only select the year, month, date, datetime in t can not be capitalized;

Time required to insert the received time converted to a string format Time String [], and then converted by the time of type String [java.util.Date] Type Date time;

Conversion is generally used in the parse method SimpleDateFormat classes:
 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ; 

Date date1=sdf.parse(startTime); 

To convert the String type time to type Date time, if the need to convert to [the year, month, day, hour, minute, second], you need to write parameter is [ "yyyy-MM-dd HH: mm: ss"]. The following form:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

The purpose of this is to allow time format and the converted database to maintain unity, such as: the length of time the same. If you do not specify the conversion process may occur Unparseable date: "2019-05-24" error;

 

Use SimpleDateFormat parse method in class when you need to consider some of the issues might be encountered. Such as string parsing waited in vain for the question format does not comply with the code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Date date1=sdf.parse(“”);

This code will throw an exception: java.text.ParseException: Unparseable date: "", in order to solve this problem and improve the robustness of the code is required

try-catch block to process, or the use of advance (startTime! = null && startTime! = "") to filter out, both may be used. code show as below:

if (startTime!=null && startTime!=""){
    try {
        Date date1=sdf.parse(startTime);
        parameterPd.put("date1",date1);
    }catch (Exception e){
        e.printStackTrace();
    }
}

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/XIAO_YAO_YOU_0/article/details/90599616
Recommended