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=0i<30i++) {//获取庆宏拦页面上下绝衫拉选项
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()
}
String str = String.format("%tD", new Data())这个只能用于格式化一个日期。
如果你旅拦要限制输入日期满足给定的格式,我觉得至少有两种解决方案:
1,使用日期控件
2,采用正则表达式判断历行输入的值是否满足给拆烂胡定的格式。
DateDropFrame extends JFrame给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 = 0i <m.lengthi++) {
m[i] = String.valueOf(i + start)
}
return m
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)