简体   繁体   中英

How to set the bootstrap date and allow the use of a date picker to change the date value?

I am dynamically building the Bootstrap HTML in JAVA with information from a database. For type='date' the date is not displayed. If I replace type='date' with type='text' the date is displayed; however, I do not have the date picker box. How can I set the date received from the database and allow the use of a date picker to change the date value?

JAVA:

    String json = null;
    String newstring = "";
    Date date = null;
    int i = 0;

    if (!ymSixList.isEmpty()) {
        for (final YthMmbrSixDtls ymSix : ymSixList) {

            if (ymSix.getYmSixStartDate() == null){
                newstring = "";
            }else{
                try {
                    date = new SimpleDateFormat("yyyy-MM-dd").parse(ymSix.getYmSixStartDate());
                    newstring = new SimpleDateFormat("dd/MM/yyyy").format(date);

                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
            }

            if (i == 0){
                //Youth encrypted member ID is stored in session storage - ssYMID

                json = "<form class='form-inline'>";
                json = json + "<select class='form-control' id='selectSixPatrol" + i + "'>";

                for (int k = 0; k < sixStoreArrayList.size(); k++) {
                    final String[] sixItem = sixStoreArrayList.get(k);

                    if (ymSix.getSixName().equals(sixItem[1])) {
                        json = json + "<option selected>" + sixItem[1] + "</option>";
                    }else{
                        json = json + "<option>" + sixItem[1] + "</option>";
                    }
                }
                json = json + "</select>";
                json = json + "<input class='form-control mr-sm-2' type='date' name='awardDate' id='awardDate' value='" + newstring + "' style='width: 180px;'>";
                json = json + "</form>";

                i++;
            }else{
                json = json + "<form class='form-inline'>";
                json = json + "<select class='form-control' id='selectSixPatrol" + i + "'>";

                for (int k = 0; k < sixStoreArrayList.size(); k++) {
                    final String[] sixItem = sixStoreArrayList.get(k);
                    if (ymSix.getSixName().equals(sixItem[1])) {
                        json = json + "<option selected>" + sixItem[1] + "</option>";
                    }else{
                        json = json + "<option>" + sixItem[1] + "</option>";
                    }
                }
                json = json + "</select>";
                json = json + "<input class='form-control mr-sm-2' type='date' name='awardDate' id='awardDate' value='" + newstring + "' style='width: 180px;'>";
                json = json + "</form>";

                i++;
            }

        }
        json = json + "<form class='form-inline'>";
        json = json + "<select class='form-control' id='selectSixPatrol" + i + "'>";

        for (int k = 0; k < sixStoreArrayList.size(); k++) {
            final String[] sixItem = sixStoreArrayList.get(k);
            json = json + "<option>" + sixItem[1] + "</option>";
        }
        json = json + "</select>";
        json = json + "<input class='form-control mr-sm-2' type='date' name='awardDate' id='awardDate' style='width: 180px;'>";
        json = json + "</form>";
    }else{

        json = "<form class='form-inline'>";
        json = json + "<select class='form-control' id='selectSixPatrol'>";

        for (int k = 0; k < sixStoreArrayList.size(); k++) {
            final String[] sixItem = sixStoreArrayList.get(k);
            json = json + "<option>" + sixItem[1] + "</option>";
        }
        json = json + "</select>";
        json = json + "<input type='date' name='awardDate' id='awardDate' style='width: 90px;'>";
        json = json + "</form>";
    }
    response.setContentType("image/jpeg");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);

答案是将日期以yyyy-MM-dd格式(而不是dd-MM-yyyy)传递给:

json = json + "<input type='date' name='awardDate' id='awardDate' style='width: 90px;'>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM