2012-02-07 179 views
2

我需要從JSpinner獲得價值。從JSpinner獲取價值(SWING)

private JSpinner[] timeSpinner; 
private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); 

Date time = new Date(); 
try { 
    time = format.parse(timeSpinner[i].getValue().toString()); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 
data[i] = time.toString(); 
System.out.println(data[i]); 

錯誤消息:

java.text.ParseException: Unparseable date: "Thu Jan 01 01:05:00 CET 1970" 
+2

爲什麼該線程已經downvoted? – 2012-02-07 14:44:34

+0

想知道downvote以及... – kleopatra 2012-02-07 14:55:54

回答

3

正在創建的SimpleDateFormat的有來自你試圖解析日期不同的格式:

new SimpleDateFormat("HH:mm:ss"); 

Thu Jan 01 01:05:00 CET 1970 

看一看的SimpleDateFormat docs

編輯:實際上看再次看起來像你已經有一個日期爲什麼不使用它呢?

time = (Date)timeSpinner[i].getValue(); 

爲了把你變成一個字符串的日期,你只需要做上面得到一個日期,然後做

String formattedDate = format.format(time); 
+0

我需要格式Thu Jan 01 01:05:00 CET 1970到「HH:mm:ss」 – 2012-02-07 13:09:26

+0

完美,您的最新更新工程。 – 2012-02-07 13:14:01

3

我看不出有任何理由對於鑄造和解析,你必須閱讀關於JSpinners的教程,並設置SpinnerDateModelJSpinner instace,那麼你可能不需要解析/鑄造Date實例JSpinner

簡單的例子

enter image description here

import java.awt.*; 
import java.text.SimpleDateFormat; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class TimeZoneSpinners { 

    private final String[] zones = {"Asia/Tokyo", "Asia/Hong_Kong", 
     "Asia/Calcutta", "Europe/Paris", "Europe/London", 
     "America/New_York", "America/Los_Angeles" 
    }; 
    private final JLabel[] labels = new JLabel[zones.length]; 
    private final SimpleDateFormat[] formats = new SimpleDateFormat[zones.length]; 
    private JSpinner spinner; 
    private SpinnerDateModel model; 
    private SimpleDateFormat format; 
    private JPanel panel; 
    private JFrame frame = new JFrame(); 

    public void makeUI() { 
     Calendar cal = Calendar.getInstance(); 
     Date date = cal.getTime(); 
     model = new SpinnerDateModel(); 
     model.setValue(date); 
     spinner = new JSpinner(model); 
     spinner.addChangeListener(new ChangeListener() { 

      @Override 
      public void stateChanged(ChangeEvent e) { 
       Date date = (Date) ((JSpinner) e.getSource()).getValue(); 
       for (int i = 0; i < labels.length; i++) { 
        labels[i].setText(formats[i].format(date)); 
       } 
      } 
     }); 
     format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat(); 
     format.setTimeZone(TimeZone.getTimeZone(zones[0])); 
     format.applyPattern("yyyy-MM-dd HH:mm:ss"); 
     panel = new JPanel(new GridLayout(zones.length, 2, 10, 10)); 
     for (int i = 0; i < zones.length; i++) { 
      formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); 
      formats[i].setTimeZone(TimeZone.getTimeZone(zones[i])); 
      JLabel label = new JLabel(zones[i]); 
      labels[i] = new JLabel(formats[i].format(date)); 
      panel.add(label); 
      panel.add(labels[i]); 
     } 
     frame.setLayout(new BorderLayout(10, 10)); 
     frame.add(spinner, BorderLayout.NORTH); 
     frame.add(panel, BorderLayout.CENTER); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TimeZoneSpinners().makeUI(); 
      } 
     }); 
    } 
}