简体   繁体   English

如何在我的选择框中列出日期格式为yyyy-mm-dd的月份值

[英]How can i list the month values in my select box with my date format yyyy-mm-dd

This is my code: 这是我的代码:

<form>
    <table>
        <tr>
            <td>Month</td>
            <td>
                <select name="">
                    <option value="">January</option>
                    <option value="">February</option>
                    <option value="">March</option>
                    <option value="">April</option>
                    <option value="">May</option>
                    <option value="">June</option>
                    <option value="">July</option>
                    <option value="">August</option>
                    <option value="">September</option>
                    <option value="">October</option>
                    <option value="">November</option>
                    <option value="">December</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Year</td>
            <td><input type="text" name="year"></td>
        </tr>
        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
</form>

I want to put the month values in the option value. 我想将月份值放在选项值中。 Let say that 2013-07-16 is the date with the date format of yyyy-mm-dd in SimpleDateFormat. 假设2013-07-16是SimpleDateFormat中日期格式为yyyy-mm-dd的日期。 How can I put the value of July or "07" in my option value with this kind of format. 如何使用这种格式将7月或“07”的值放入我的选项值中。

<fmt:format var="month" value="${theDate}" pattern="MM"/>
<select name="">
    <option value="01" ${month == "01" ? "selected='selected'" : ""}>January</option>
    <option value="02" ${month == "02" ? "selected='selected'" : ""}>February</option>
    ...
</select>

There is a lot of repetition, though, in this code, so you'd better prepare a List<SelectableMonth> in your controller, and iterate over this list: 但是,在此代码中有很多重复,因此您最好在控制器中准备List<SelectableMonth> ,并迭代此列表:

 <select name="">
    <c:forEach var="month" items="${selectableMonths}">
        <option value="${month.number}" ${month.selected ? "selected='selected'" : ""}>${month.label}</option>
    </c:forEach>        
</select>

I haven't written any JSP in years but I think this should point you in the right direction: 我多年没有编写任何JSP,但我认为这应该指向正确的方向:

<%@ taglib uri="http://java.sun.com/jstl/core"    prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>

<%!
  String[] months = { "January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
%>

<form>
    <table>
        <tr>
            <td>Month</td>
            <td>
                <select name="">
                  <c-rt:forEach var="month" items="<%= months %>">
                      <option value="${i}">${months[i]}</option>
                  </c-rt:forEach>
                </select>
            </td>
        </tr>
        <tr>
            <td>Year</td>
            <td><input type="text" name="year"></td>
        </tr>
        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
</form>

Let say that 2013-07-16 is the date with the date format of yyyy-mm-dd in SimpleDateFormat. 假设2013-07-16是SimpleDateFormat中日期格式为yyyy-mm-dd的日期。 How can I put the value of July or "07" in my option value with this kind of format. 如何使用这种格式将7月或“07”的值放入我的选项值中。

Your question is a little unclear, but it sounds like you're asking if you have a date value modelled by a String , and a SimpleDateFormat that matches the pattern of that value, how can you modify the format to output it in a new way? 你的问题有点不清楚,但听起来你问的是你是否有一个由String建模的日期值,以及一个匹配该值模式的SimpleDateFormat ,你如何修改格式以便以新的方式输出它? Is that what you're looking for? 这就是你要找的东西吗?

If that is what you're asking, you'd need twwo more instances of SimpleDateFormat to model the new formats you want to use — one to model the month name and other for its numeric value. 如果这就是您所要求的,那么您需要两个以上的SimpleDateFormat实例来建模您想要使用的新格式 - 一个用于建模月份名称,另一个用于数值。 You could then use the parse(String source) method to turn your String into a Date & reformat it from there. 然后,您可以使用parse(String source)方法将String转换为Date并从那里重新格式化它。 If you want to take this approach, something like the below should work. 如果你想采用这种方法,下面的内容应该可行。

Be careful of throwing a ParseException if taking this approach though. 如果采用这种方法,请注意抛出ParseException You'd be better to start from a Date instead, if possible. 如果可能的话,你最好从Date开始。

<%
    String today = "2013-07-16";
    SimpleDateFormat yearMonthDay = new SimpleDateFormat("yyyy-MM-dd");
    Date todaysDate = yearMonthDay.parse(today);

    String monthAsNumber = new SimpleDateFormat("MM").format(todaysDate);
    String monthAsFullName = new SimpleDateFormat("MMMMM").format(todaysDate);
%>

<select>
    <option value="<%= monthAsNumber %>"><%= monthAsFullName %></option>
</select> 

Also, it would be best practice to remove this type of Java code from the JSP altogether for testability, re-use etc — You should look into using JSTL functions, that will allow you to move the data parsing out into a separate class & then use a bean to render out the two values you need to display per date. 此外,最好从JSP中删除这种类型的Java代码以实现可测试性,重用等 - 您应该考虑使用JSTL函数,这将允许您将数据解析移动到单独的类中然后使用bean来渲染每个日期需要显示的两个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用YYYY-MM-DD格式让SQLite按日期排序? - How can i get SQLite to sort by date properly using YYYY-MM-DD format? 如何将日期类型参数作为字符串放入 GET 请求中? 格式“yyyy-MM-dd” - How can I put Date type parameter as a String in GET request? Format "yyyy-MM-dd" 如何使用日期类获取格式为“ YYYY-MM-DD 00:00:00.0”的数据? - How can i get data in format “YYYY-MM-DD 00:00:00.0” using class Date? 如何将用户以MM-dd-yyyy格式选择的日期转换为Java中的yyyy-MM-dd格式 - How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java 如何在yyyy-mm-dd中正确格式化日期? - How to properly format Date in yyyy-mm-dd? 如何将包含日期的字符串值格式化为yyyy-mm-dd? - how to format a string value containing date to yyyy-mm-dd? 如何将日期转换为 yyyy-MM-dd 格式? - How to convert date in to yyyy-MM-dd Format? 如何初始化日期为YYYY-MM-DD格式的公历? - How to initialize Gregorian calendar with date as YYYY-MM-DD format? 我需要将日期格式从 dd-MM-yyyy 更改为 yyyy-MM-DD - I need to change date format from dd-MM-yyyy to yyyy-MM-DD 如何以“ yyyy-mm-dd”格式将日期列表放入JComboBox? - How to put list of dates in JComboBox in 'yyyy-mm-dd' format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM