重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
DateDropFrame extends JFrame
我们提供的服务有:网站制作、成都网站制作、微信公众号开发、网站优化、网站认证、尼木ssl等。为上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的尼木网站制作公司
给year和month添加一个ItemListener来实时计算指定年和月中有多少天就可以了.
代码是用Swing写的,不过看你的图,不像是Swing界面.
/**
* Create the frame.
*/
public DateDropFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
final JComboBox year = new JComboBox();
year.setModel(new DefaultComboBoxModel(getModel(start, end)));
contentPane.add(year);
final JComboBox month = new JComboBox();
month.setModel(new DefaultComboBoxModel(getModel(1, 12)));
contentPane.add(month);
final JComboBox day = new JComboBox();
contentPane.add(day);
year.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDay(year, month, day);
}
});
month.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDay(year, month, day);
}
});
setDay(year, month, day);
setContentPane(contentPane);
}
/**
* calculate days in select month year
*/
private void setDay(JComboBox year, JComboBox month, JComboBox day) {
int y = Integer.parseInt((String) year.getSelectedItem());
int m = Integer.parseInt((String) month.getSelectedItem());
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, y);
c.set(Calendar.MONTH, m - 1);
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
day.setModel(new DefaultComboBoxModel(getModel(1, days)));
}
/**
* get String array [start, end]
*/
private String[] getModel(int start, int end) {
String[] m = new String[end - start + 1];
for (int i = 0; i m.length; i++) {
m[i] = String.valueOf(i + start);
}
return m;
}
private void changeDayComboBox(String dyear,String dmouth){
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
Calendar rightNow = Calendar.getInstance();
try{
rightNow.setTime(simpleDate.parse(dyear+"/"+dmouth));
}catch(ParseException e){
e.printStackTrace();
}
int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);//根据年月 获取月份天数
DefaultComboBoxModel list = new DefaultComboBoxModel();
for(int i = 0; i days; i++){
list.addElement(i+1);
}
m_DayComboBox.setModel(list);
调用
[java] view plain copy
changeDayComboBox("2015","7");
String[] arry = new String[201];
for (int i = 2000; i 2201; i++) {
arry[i-2000] = i+"年";
System.out.println(arry[i-2000]);
}
JComboBox combox = new JComboBox(arry);
你是要30天的日期嘛。。。
public String genRqOpts() {
StringBuilder rtn = new StringBuilder("");
java.util.Date date = new java.util.Date();//获得当前系统时间
SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
String time = simple.format(date);
String cur_Y = time.substring(0,4);//年
String cur_M = time.substring(4,6);//月
String cur_D = time.substring(6,8);//日
Calendar calendar = Calendar.getInstance();//初始化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
calendar.set(Integer.parseInt(cur_Y), Integer.parseInt(cur_M), Integer.parseInt(cur_D));
rtn.append("select");
for (int i=0;i30;i++) {//获取页面上下拉选项
calendar.add(Calendar.DATE, -1);
String elRq = sdf.format(calendar.getTime());
rtn.append("option value='");
rtn.append(elRq);
rtn.append("'");
rtn.append(elRq);
rtn.append("/option");
}
rtn.append("/select");
return rtn.toString();
}
将这个包解压 将文件放到WebContent中 然后到jsp 导入
script type="text/javascript" src="/Rebates/My97DatePicker/WdatePicker.js"/script
这句代码 由于 我的是WebContent下面还有个Rebates文件夹 所以路径是这样
使用的时候
input value="${startTime }" id="stime" name="startTime" onclick="WdatePicker({startDate:'%y-%M-01 10:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"
onclick事件 就可以了