html 如何将周数的倍数(1月1日= 1,12月25日= 52)转换为开始日期和结束日期来过滤jsp报告?

szqfcxe2  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(92)

需要指导如何更改the UI of the multiple select of weekly first dates中所示的每个选项的第一个日期的值。每个日期包含周数value contains in each option的值。预期用途是允许用户选择多个第一个日期,然后选择一个年份,之后,他们点击搜索按钮。我尝试了DatesGetTotalDaysInMonth和Dates。getWeekStartDate,但它得到了错误,我将详细说明如下:
在报告中。jsp:

out.println("<div class='grid_3' id='hidden_div' style='display:none;'>");
    out.println("<p>");
    
    out.println("<select name='weekStart' id= 'weekStart' style='width:150px;' multiple='multiple' >");
    
    String weekDates[]={"01-02","01-09","01-16","01-23","01-30","02-06","02-13","02-20","02-27",
            "03-06","03-13","03-20","03-27","04-03","04-10","04-17","04-24","05-01","05-08",
            "05-15","05-22","05-29","06-05","06-12","06-19","06-26","07-03","07-10","07-17","07-24",
            "07-31","08-07","08-14","08-21","08-28","09-04","09-11","09-18","09-25","10-02","10-09","10-16",
            "10-23","10-30","11-06","11-13","11-20","11-27","12-04","12-11","12-18","12-25"};
    int numWeeksPerMth[] = {5, 4, 4, 4, 5,4, 4, 5, 4, 5, 4, 4};
//  int numOfWeek[]= {"Jan1", "Jan2", "Jan3", "Jan4", "Jan5", "Feb6", "Feb7", "Feb8", "Feb9", "Mar10", "Mar11", "Mar12", "Mar13", "Apr14", "Apr15", "Apr16", "Apr17",
//                   "May18", "May19", "May20", "May21", "May22", "Jun23", "Jun24", "Jun25", "Jun26", "Jul27", "Jul28", "Jul29", "Jul30",
//                   "Aug31", "Aug32", "Aug33", "Aug34", "Aug35", "Sep36", "Sep37", "Sep38", "Sep39", "Oct40", "Oct41", "Oct42", "Oct43", "Oct44",
//                   "Nov45", "Nov46", "Nov47", "Nov48", "Dec49", "Dec50", "Dec51", "Dec52"};
    String weekNum[]= {"Jan1", "Jan2", "Jan3", "Jan4", "Jan5", "Feb6", "Feb7", "Feb8", "Feb9", "Mar10", "Mar11", "Mar12", "Mar13", "Apr14", "Apr15", "Apr16", "Apr17",
                     "May18", "May19", "May20", "May21", "May22", "Jun23", "Jun24", "Jun25", "Jun26", "Jul27", "Jul28", "Jul29", "Jul30",
                     "Aug31", "Aug32", "Aug33", "Aug34", "Aug35", "Sep36", "Sep37", "Sep38", "Sep39", "Oct40", "Oct41", "Oct42", "Oct43", "Oct44",
                     "Nov45", "Nov46", "Nov47", "Nov48", "Dec49", "Dec50", "Dec51", "Dec52"};
    for (int k = 1; k < weekNum.length; k++){
        out.println("<option value =" +k+">"+weekDates[k]+"</option>");
        
    }
    out.println("</select>");
    out.println("</p>");
    out.println("<p>");
    out.println("<label>Year</label>");
//                              out.println("<td>");
                                 out.println("<select name='year' id='year' class='mediumSelect'>");
                                
                                String currentYear = Dates.getTimeStamp("yyyy");
                            
                                for(int years=2020;years<(Integer.parseInt(currentYear)+5);years++) {
                                    
                                    if(Integer.parseInt(currentYear) == years){
                                        out.println("<option value='"+years+"' selected>"+ years +"</option>");
                                    }else{
                                        out.println("<option value='"+years+"'>"+ years +"</option>");
                                    }
                                    }
                                
    out.println("</select>");
//  out.println("</td>");
    out.println("</p>");
    out.println("</div>");
//this is in the action page(report_act.jsp)
if(!monthStart.equals("")){
        for(int t=1;t<(totalMonth);t++){
        
        startDate=""+monthStart+"-01";
        int totalDays = Dates.getTotalDaysInMonth(monthEnd+"-"+t+"-01");
        endDate=""+monthEnd+"-"+totalDays;
        }
        response.sendRedirect(""+namePage+".jsp?millId="+millId+"&stationStatus="+stationStatus+"&inspectorId="+inspectorId+"&roundIdList="+roundIdList+"&reportType="+reportType+"&jobPosition="+jobPosition+"&scheduleId="+scheduleId+"&filterType="+filterType+"&monthStart="+monthStart+"&monthEnd="+monthEnd+"&startDate="+startDate+"&endDate="+endDate+"&roundId="+roundId+"&stationId="+stationId+"&sequenceId="+sequenceId+"&pageNo="+pageNo+"");
    }
    
    else{
        for(int t=1;t<=(totalWeek);t++){
            
            startDate= year+"-"+weekStart;
            //String weekDate = Dates.getDayOfWeek(weekStart+"-"+t);
            //endDate= year+"-"+weekStart+"-"+weekDate;
            LogFunction.loginfo(Dates.getDayOfWeek(weekStart+"-"+t));
            }
        
            response.sendRedirect(""+namePage+".jsp?millId="+millId+"&stationStatus="+stationStatus+"&inspectorId="+inspectorId+"&roundIdList="+roundIdList+"&reportType="+reportType+"&jobPosition="+jobPosition+"&scheduleId="+scheduleId+"&filterType="+filterType+"&weekStart="+weekStart+"&startDate="+startDate+"&endDate="+endDate+"&roundId="+roundId+"&stationId="+stationId+"&sequenceId="+sequenceId+"&pageNo="+pageNo+"");
        }
// the method i used in Dates.java
 public static String getDayOfWeek(String dateString) {
    // for GMT+8:00
      //gets date plus or minus x months
      java.util.Date date=new java.util.Date();
      SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
      try{
        date=df.parse(dateString);
      }catch(Exception e){}

      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      String datestr = Integer.toString(calendar.get(Calendar.DAY_OF_WEEK));
     // calendar.add(Calendar.MONTH,x);
     // df = new SimpleDateFormat("yyyy-MM-dd");
     // String datestr = df.format(calendar.getTime());

    return datestr;
  }
bmvo0sr5

bmvo0sr51#

Java.时间

***java.util***日期时间API及其格式化API SimpleDateFormat已经过时,容易出错,建议完全停止使用,改用modern date-time API

使用java.time(现代日期-时间API)的解决方案

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        String weekDay = "01-02";
        String[] parts = weekDay.split("-");
        int week = Integer.parseInt(parts[0]);
        int day = Integer.parseInt(parts[1]);

        WeekFields weekFields = WeekFields.of(Locale.US);

        LocalDate date = LocalDate.now()
                .withYear(2023) // Replace 2023 with Year.now().getValue() for the current year
                .with(weekFields.weekOfYear(), week)
                .with(weekFields.dayOfWeek(), day);
        System.out.println(date);
    }
}

输出

2023-01-02

从**Trail: Date Time**了解有关现代日期-时间API的更多信息。

相关问题