DateDropFrame extends JFrame
我們提供的服務有:網(wǎng)站制作、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、尼木ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的尼木網(wǎng)站制作公司
給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);//根據(jù)年月?獲取月份天數(shù)??
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();//獲得當前系統(tǒng)時間
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事件 ? 就可以了