【问题标题】:How to put list of dates in JComboBox in 'yyyy-mm-dd' format?如何以'yyyy-mm-dd'格式将日期列表放入JComboBox?
【发布时间】:2013-02-10 00:12:52
【问题描述】:

我正在尝试使用此代码将当前日期和第二天的日期放在 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-m-d”格式显示日期,我希望它以“yyyy-mm-dd”格式显示。

我觉得可以用

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

以 'yyyy-mm-dd' 格式获取当前日期,但如何处理第二天的日期?

【问题讨论】:

    标签: java swing date jcombobox date-format


    【解决方案1】:
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1); //next day
    cal.getTime(); // next day's date  
    

    您需要将格式更改为 yyyy-MM-dd 以获得所需的格式

    【讨论】:

      【解决方案2】:

      您应该将日期对象添加到 JComboBox 中,而不是当前日期的字符串和下一天的日期,然后使用自定义的 ListCellRenderer 以所需格式呈现日期。

      示例代码:

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

      【讨论】:

        【解决方案3】:

        我通常从不向 JComboBox 中添加字符串;相反,我定义了一个 data 对象,其中包含所需类型的成员(在您的情况下为 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();
        

        【讨论】:

        • 不,不需要人工对象 - Swing 处理自定义渲染的方式是......自定义渲染器
        猜你喜欢
        • 2021-05-10
        • 2016-01-02
        • 1970-01-01
        • 1970-01-01
        • 2020-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多