简体   繁体   中英

Creating a simple timesheet in HTML/Java

I'm trying to make a simple timesheet and I got stuck. I'm trying to ONLY show the months between 2 days ( finalDayEnd and initialDayEnd ).

I already got the dates via my session variable.

在此处输入图片说明

Example: Let's say initialDayEnd is 04/10/2017 and finalDayEnd is 01/01/2018

My Month Tag Should display October,November,December for 2017 and January for 2018. In this case I would need to start table days in 4 instead of 1 for month October and end in 01 for January 2018.

How can I get the Months and years between 2 dates and how to start on the initialDay and end of finalDay.

<div class="row" style="margin-top: 30px;">
    <div class="tab-content">
        <div id="Individual" class="tab-pane fade in active">
            <div class="col-md-6">
                <div class="row">
                    <%
                        String initialDayEnd = session.getAttribute("initialDayEnd").toString();
                        String finalDayEnd = session.getAttribute("finalDayEnd").toString();
                    %>
                    <div class="dropdown">
                        <label>Month:</label>
                        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">January
                        <span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a href="#">January</a></li>
                            <li><a href="#">February</a></li>
                            <li><a href="#">March</a></li>
                        </ul>
                    </div>
                </div>
                <div class="row" style="margin-top: 10px;">
                    <div class="dropdown">
                        <label>Year:</label>
                        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">2017
                            <span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a href="#">2017</a></li>
                            <li><a href="#">2018</a></li>
                            <li><a href="#">2019</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="col-md-6">

            </div>
        </div>
    </div>
    </div>
    <div class="row" style="margin-top: 30px;">
            <div class="table-responsive">
                <%
                    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                    SimpleDateFormat ft  = new SimpleDateFormat("EEEE", Locale.ENGLISH);
                    LocalDate localDate = LocalDate.of(2017,1,19);
                    int days = localDate.lengthOfMonth();
                    Date date;
                %>
                <table class="table">
                    <thead>
                    <tr>
                        <th class="text-center">#</th>
                        <th class="text-center">Day</th>
                        <th class="text-center">Number of Clients</th>
                        <th class="text-center"></th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        for(int day = 1;day<=days;day++) {
                            date = sdf.parse(day+"/10/2017");
                    %>
                    <tr class="text-center">
                        <td><%=day%></td>
                        <td><%=ft.format(date)%></td>
                        <td><input name="amountOfClients[]" type="number"></td>
                        <td>
                            <%
                                if(day == 1) {
                            %>
                            <input type="checkbox" <%--<%= environment.isPaused() ? "checked='checked'" : ""%>--%> data-toggle="toggle" data-size="small" value="Repeat">
                            <label> Repeat for All?</label>
                            <%
                                }
                            %>
                        </td>
                    </tr>
                    <%
                        }
                    %>
                    </tbody>
                </table>
            </div>
        </div>

The first thing you should do is convert those string values to actual date values, it'll make any math a lot easier. Something like this:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date startDate = format.parse(initialDayEnd);
Date endDate = format.parse(finalDayEnd);

From there you could use a loop from one date to the next, incrementing by month, and adding the name of the month to an ArrayList . As a quick example, something like this:

ArrayList<String> months = new ArrayList<String>();
while (startDate.before(endDate)) {
    months.add(new DateFormatSymbols().getMonths()[startDate.getMonth()]);
    startDate.setMonth(startDate.getMonth() + 1);
}

Afterward, the months list would have the names of all the months between the start date and the end date.

(Note: I'm getting deprecation warnings on getMonth() and setMonth() . There appear to be updated versions using a Calendar object, with which I am unfamiliar. My Java-specific knowledge is a bit dated, but the concept is still the same regardless of the objects used.)

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