简体   繁体   中英

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

I am trying to put current date and the date of the next day in a JComboBox with this code

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-md' format and I want it in 'yyyy-mm-dd' format.

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?

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

You should add Date Objects into JComboBox instead of the String for current Date and date for the next day and then use customized ListCellRenderer for rendering the Date in desired format.

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

}

I usually never add strings to a JComboBox ; instead I define a data object which contains a member of the desired type ( Date in your case) and override the toString method to determine how it will appear in the 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:

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 object is that you can easily retrieve the selected date from the JComboBox , instead of its String representation.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM