2013-03-22 116 views
2

我有一個JSpinner。當我使用spinner.getValue()它給了我Sun Mar 24 01:11:41 PDT 2013 ,但我想只有Hour and Minute in 24 hour format如何獲得JSpinner的小時和分鐘?

Date date = new Date(); 
     SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.MINUTE); 

     JSpinner spinner = new JSpinner(sm); 
     JSpinner.DateEditor de = new JSpinner.DateEditor(spinner, "hh:mm a"); 
     spinner.setEditor(de); 
     if (spinner.getEditor() instanceof JSpinner.DefaultEditor) 
     { 
        JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) spinner.getEditor(); 
        editor.getTextField().setEnabled(true); 
        editor.getTextField().setEditable(false); 
     } 

     System.out.println("Spinner:  "+spinner.getValue()); 

回答

7

從@ mKorbel提取肉的回答:

System.out打印值對象uing其toString方法,如果要打印格式化您需要使用Formatter,f.i.一個在你的代碼中使用由編輯:

Date date = new Date(); 
SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.MINUTE); 

JSpinner spinner = new JSpinner(sm); 
JSpinner.DateEditor de = new JSpinner.DateEditor(spinner, "hh:mm a"); 
de.getTextField().setEditable(false); 
spinner.setEditor(de); 

System.out.println("Spinner:  "+de.getFormat().format(spinner.getValue())); 
+0

謝謝。它可以工作,但不是24小時模式。 – Salman 2013-03-23 08:19:10

+0

這是因爲您配置了格式以顯示它:-)可以將其配置爲顯示24h或使用單獨的格式(如果您希望它與微調器的格式不同 – kleopatra 2013-03-23 10:16:52

+2

我在24小時模式中使用「HH:mm」 .--) spinnerHours.setEditor(new JSpinner.DateEditor(spinnerHours,「HH:mm」)); – 2013-09-10 10:55:49

5
  • 必須使用SimpleDateFormat的用於格式化

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"); 
     format.applyPattern("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] = new SimpleDateFormat("HH:mm:ss"); 
      formats[i].setTimeZone(TimeZone.getTimeZone(zones[i])); 
      JLabel label = new JLabel(zones[i]); 
      labels[i] = new JLabel(formats[i].format(date)); 
      labels[i].setHorizontalAlignment(JLabel.RIGHT); 
      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(); 
      } 
     }); 
    } 
} 
+3

隱藏重要的東西以噸(concededly不錯:-)混亂的一次?提醒:最佳答案爲SSCCE爲問題代碼 – kleopatra 2013-03-22 10:16:21

0
public static String convertTo12Hour(String Time){ //20:00:00 
    String input = Time; 
    //Format of the date defined in the input String 
    DateFormat df = new SimpleDateFormat("HH:mm:ss"); 
    //Desired format: 24 hour format: Change the pattern as per the need 
    DateFormat outputformat = new SimpleDateFormat("hh:mm:ss aa"); 
    Date date = null; 
    String output = null; 
    try { 
    date= df.parse(input); 
} catch (ParseException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
    //Changing the format of date and storing it in String 
    output = outputformat.format(date); 
    return output; //8:00:00 

}

+0

24小時轉換爲12小時的甲酸酯 – azhar 2016-12-21 10:35:06

相關問題