簡體   English   中英

如何以“ yyyy-mm-dd”格式將日期列表放入JComboBox?

[英]How to put list of dates in JComboBox in 'yyyy-mm-dd' format?

我正在嘗試使用此代碼將當前日期和第二天的日期放入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));
}

但是它以'yyyy-md'格式顯示日期,我想要'yyyy-mm-dd'格式。

我想我可以用

Date date = new Date();
SimpleDateFormat  sdf = new SimpleDateFormat("yyyy/MM/dd");
txt_date.setText(sdf.format(date));

要以“ yyyy-mm-dd”格式獲取當前日期,但第二天的日期該怎么辦?

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1); //next day
cal.getTime(); // next day's date  

您需要將格式更改為yyyy-MM-dd

您應該將Date對象而不是當前日期的String和第二天的日期添加到JComboBox中 ,然后使用自定義的ListCellRenderer以所需的格式呈現Date。

樣例代碼:

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 );
    }

}

我通常從不向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);
    }
}

現在,您可以按照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);
}

使用數據對象的優點是,您可以輕松地從JComboBox而不是其String表示中檢索所選日期。

DateItem di = (DateItem)cb.getSelectedItem();
Date d = di.getDate();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM