java 如何以'yyyy-mm-dd'格式在JComboBox中放置日期列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14793146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put list of dates in JComboBox in 'yyyy-mm-dd' format?
提问by Raed Shahid
I am trying to put current date and the date of the next day in a JComboBox with this code
我正在尝试使用此代码将当前日期和第二天的日期放入 JComboBox
private void dateCombo(){
Calendar cal = new GregorianCalendar();
int month =cal.get(Calendar.MONTH);
int year =cal.get(Calendar.YEAR);
int day =cal.get(Calendar.DAY_OF_MONTH);
cmb_date.addItem(+year+"-"+(month+1)+"-"+day);
cmb_date.addItem(+year+"-"+(month+1)+"-"+(day+1));
}
But it is showing the date in 'yyyy-m-d' format and I want it in 'yyyy-mm-dd' format.
但它以“yyyy-md”格式显示日期,我希望它以“yyyy-mm-dd”格式显示。
I think i can use
我想我可以用
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
txt_date.setText(sdf.format(date));
To get the current date in 'yyyy-mm-dd' format but what to do about the date of the next day?
要以 'yyyy-mm-dd' 格式获取当前日期,但如何处理第二天的日期?
回答by Jigar Joshi
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1); //next day
cal.getTime(); // next day's date
and you need to change format to yyyy-MM-dd
for your desired format
并且您需要将格式更改yyyy-MM-dd
为所需的格式
回答by Gnanasekaran Palanisamy
You should add Date Objects into JComboBoxinstead of the String for current Date and date for the next day and then use customized ListCellRendererfor rendering the Date in desired format.
您应该将日期对象添加到JComboBox而不是当前日期的字符串和第二天的日期,然后使用自定义的ListCellRenderer以所需格式呈现日期。
Sample Code:
示例代码:
import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
public class DateComboExample {
// Create Date Renderer for formatting Date
public static class DateComboBoxRenderer extends DefaultListCellRenderer {
// desired format for the date
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) {
Object item = value;
// if the item to be rendered is date then format it
if( item instanceof Date ) {
item = dateFormat.format( ( Date ) item );
}
return super.getListCellRendererComponent( list, item, index, isSelected, cellHasFocus);
}
}
public static void main( String[] str ) {
JComboBox combo = new JComboBox();
// Add current date
GregorianCalendar calendar = new GregorianCalendar();
combo.addItem( calendar.getTime() );
// Add Next date
calendar.roll( GregorianCalendar.DAY_OF_MONTH, 1 );
combo.addItem( calendar.getTime() );
// Set Renderer for formating the date in combobox
combo.setRenderer( new DateComboBoxRenderer() );
JFrame frame = new JFrame( "Date Rendere Example" );
JPanel panel = new JPanel();
panel.add( new JLabel( "Date Combo: ") );
panel.add( combo );
frame.add( panel );
frame.pack();
frame.setVisible( true );
}
}
回答by damix911
I usually never add strings to a JComboBox; instead I define a data objectwhich contains a member of the desired type (Datein your case) and override the toStringmethod to determine how it will appear in the JComboBox.
我通常从不向JComboBox添加字符串;相反,我定义了一个数据对象,其中包含所需类型的成员(在您的情况下为Date)并覆盖toString方法以确定它在JComboBox 中的显示方式。
public class DateItem {
private Date mDate;
public DateItem(Date date) {
mDate = date;
}
public Date getDate() {
return mDate;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(mDate);
}
}
Now you can insert all the days you want into the JComboBox by following the pattern that Jigar Joshi shown in his answer. Here I'm using it to add four weeks in total:
现在,您可以按照 Jigar Joshi 在他的回答中显示的模式将您想要的所有天数插入 JComboBox。在这里,我使用它总共增加了四个星期:
JComboBox cb = new JComboBox();
Calendar calendar = Calendar.getInstance();
for (int i = 0; i < 28; ++i) {
cb.addItem(new DateItem(calendar.getTime()));
calendar.add(Calendar.DATE, 1);
}
The advantage of using a data objectis that you can easily retrieve the selected date from the JComboBox, instead of its Stringrepresentation.
使用数据对象的优点是您可以轻松地从JComboBox 中检索所选日期,而不是从它的String表示中检索。
DateItem di = (DateItem)cb.getSelectedItem();
Date d = di.getDate();