天道酬勤,学无止境

How change format time from jSpinner?

I use a jSpinner to choose and save time.

I want enter the start time for a task in the database. But the format that saves the database is this: Tue Apr 15 17:24:56 BST 2014

I want in HH:mm.

This is the code I use to write the data in the database:

    public void UpdateTask() {
    try {
        Task t1 = new Task();
        t1.setIdTask(jTIdTask.getText());
        t1.setDate(jTDate.getText());

        ---->  t1.setHourBegin(jSpinner2.getValue().toString());  // This is what I need change

        TaskDao dao = new TaskDao();
        dao.updateTask(t1);

    } catch (SQLException ex) {
        Logger.getLogger(jTTask.class.getName()).log(Level.SEVERE, null, ex);
    }
}

CLASS DAO

  public void updateTask(Taskst1) throws SQLException{
    String sql = "update Taskset idTask=?, date=?, hourBegin=? where idTask=?"; 
    PreparedStatement stmt = this.conexao.prepareStatement(sql);
    stmt.setString(1, st1.getIdTask());
    stmt.setString(2, st1.getDate());
    stmt.setString(3, st1.getHourBegin());
    stmt.setString(4, st1.getIdTask());

    stmt.execute();
    stmt.close();
    conexao.close();
}

FORM

enter image description here

enter image description here


SOLUTION

   public void UpdateTask() {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Task t1 = new Task();
        t1.setIdTask(jTIdTask.getText());
        t1.setDate(jTDate.getText());

        String time = sdf.format(jSpinner2.getValue());
        t1.setHourBegin(time);

        TaskDao dao = new TaskDao();
        dao.updateTask(t1);

    } catch (SQLException ex) {
        Logger.getLogger(jTTask.class.getName()).log(Level.SEVERE, null, ex);
    }
}

评论

jSpinner2.getValue().toString() is basically asking the returned object for whatever String representation it has, in this instance, you're asking a Date object to pass you back it's default String representation.

Remember, Date does not have any concept of a format, it's just a container for the number of milliseconds since the Unix epoch.

You will need to use a DateFormatter of some kind to format the Date value from the JSpinner to the format you want...

A better solution would be to use some kind of date/time data type in you database...

Updated

The stored in the JSpinner is a Date object, it has no concept of a format. You will need to use some kind of DateFormatter to take to from been a Date to been a String value formatted the way you want.

Object value = jSpinner2.getValue();
if (value instanceof Date) {
    String time = new SimpleDateFormat("HH:mm").format(value);
    t1.setHourBegin(time);
} else {
    // Deal with the invalid value...
}

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • 如何设置 jSpinner 默认值?(how to set jSpinner default value?)
    问题 我正在使用 jSpinner 设置格式为“HH:MM”的时间,它工作正常。 我的问题是我想在激活窗口时将 jSpinner 设置为“00:00”,一旦单击 jSpinner,我希望将值更改为“HH:MM”格式。 而且我自己做不到,jSpinner 值不会改变它仍然是“00:00”。 请帮我。 先感谢您。 回答1 假设您使用的是SpinnerDateModel ,那么JSpinner管理的值是java.util.Date 。 您需要创建一个Date实例,它的小时/分钟字段设置为午夜 Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); Date date = cal.getTime(); spinner.setValue(date); 要从JSpinner获取格式值,您可以使用SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); String value = sdf.format(spinner.getValue()); 但是当窗口被激活时我如何设置它为“00:00” 没有更多信息,我建议使用某种WindowListener
  • Java中的JSpinner.DateEditor在初始化时不尊重时区(JSpinner.DateEditor in Java Not Respecting TimeZone on Initialization)
    问题 我正在对使用 java 日期和 Swing 的 JSpinner 作为 DateEditor 的现有 Swing 应用程序进行错误修复。 我试图让编辑器默认使用 UTC 来显示时间,而不是我们的本地时区。 该应用程序使用 Java 8 在 Windows 上运行。 我正在使用的代码如下。 import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; public class Test { public static void main(String [] args) { // Initialize some sample dates Date now = new Date(System.currentTimeMillis()); JSpinner spinner = new JSpinner(); // Create model with a current date and no start/end date boundaries
  • JSpinner 给出旧值(JSpinner giving old values)
    问题
  • JSpinner time range + HH:MM format
    I'm trying to set a JSpinner to run from hour 00:00 to 02:00. So I built this code: import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; public class HourSpinner2 extends javax.swing.JFrame { javax.swing.JSpinner jSpinner2; public HourSpinner2() { jSpinner2 = new javax.swing.JSpinner(); add(jSpinner2); // Option 1 : set range jSpinner2.setModel(new SpinnerDateModel(new java.util.Date(1388498400000L), new java.util.Date(1388484000000L), new java.util.Date(1388505600000L), java.util.Calendar.MINUTE)); // Option 2 : set HH:mm format jSpinner2.setModel(new SpinnerDateModel(new java
  • JSpinner 时间范围 + HH:MM 格式(JSpinner time range + HH:MM format)
    问题 我正在尝试将JSpinner设置为从 00:00 到 02:00 运行。 所以我构建了这个代码: import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; public class HourSpinner2 extends javax.swing.JFrame { javax.swing.JSpinner jSpinner2; public HourSpinner2() { jSpinner2 = new javax.swing.JSpinner(); add(jSpinner2); // Option 1 : set range jSpinner2.setModel(new SpinnerDateModel(new java.util.Date(1388498400000L), new java.util.Date(1388484000000L), new java.util.Date(1388505600000L), java.util.Calendar.MINUTE)); // Option 2 : set HH:mm format jSpinner2.setModel(new SpinnerDateModel(new java.util.Date(1388498400000L), null
  • JSpinner giving old values
    I am using couple of JSpinners in my project who display the hour and minutes. When the JSpinner is incremented or decremented i have to save the value to the database. But the problem is that the JSpinners are giving me old values. eg- If the time displayed is 09:30 and i increment the time to 10:30, I am getting 09:30 as the returned value. I am using following code UPDATED SSCCE package spinnerupdation; import java.awt.Container; import java.awt.FlowLayout; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import
  • 如何从我的 JSpinner 获取日期?(How to get Date from my JSpinner?)
    问题 这是微调器的代码 Hini = new javax.swing.JSpinner(); Date date = new Date(); SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.MINUTE); JSpinner Hini = new JSpinner(sm); JSpinner.DateEditor de = new JSpinner.DateEditor(Hini, "hh:mm a"); de.getTextField().setEditable( true ); Hini.setEditor(de); 这就是我希望它获取值的方式,但它始终显示“00:00” `SimpleDateFormat formater = new SimpleDateFormat("HH/mm"); String spinnerValue = formater.format(Hini.getValue()); System.out.println(spinnerValue);` 我也试过这个,但它总是显示实际时间,而不是我选择的时间 Hini = new JSpinner(sm); de = new JSpinner.DateEditor(Hini, "hh:mm a"); de
  • JSpinner.DateEditor 必须包含年份,即使开始和结束是同一年(JSpinner.DateEditor must include year even though start and end is the same year)
    问题 我有一个使用 SpinnerDateModel 的 JSpinner,它从 2010 年 1 月 1 日 00:00:00.000 开始,结束日期是 2010 年 1 月 1 日 00:12:34.217。 我希望我的 JSpinner.DateEditor 使用格式 HH:mm:ss.SSS 但微调器不使用这种格式旋转。 它仅在“yyyy”添加到格式时旋转。 我怎样才能解决这个问题? import java.awt.GridLayout; import java.util.*; import javax.swing.*; public class T extends JPanel { public T() { super(new GridLayout(2, 2)); init(); } private void init() { Calendar start = GregorianCalendar.getInstance(); Calendar end = GregorianCalendar.getInstance(); start.clear(); end.clear(); start.set(Calendar.YEAR, 2010); end.set(Calendar.YEAR, 2010); end.add(Calendar.HOUR_OF_DAY, 12)
  • how to set jSpinner default value?
    I'm using a jSpinner to set the time with format "HH:MM" and it works fine. My problem is I want to set my jSpinner to "00:00" when the window is activated, and once jSpinner is clicked I want the value to be changed to the "HH:MM" format. And I can't do it myself, the jSpinner value doesn't change it is still "00:00". Please help me. Thank you in advance.
  • 从 JSpinner (SWING) 获取价值(Getting the value from JSpinner (SWING))
    问题 我需要从 JSpinner 获得价值。 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" 回答1 您正在创建的 SimpleDateFormat 与您尝试解析的日期格式不同: new SimpleDateFormat("HH:mm:ss"); Thu Jan 01 01:05:00 CET 1970 看看 SimpleDateFormat 文档 编辑:实际上再看一遍,您似乎已经有了一个 Date 对象,为什么不直接使用它呢? time = (Date)timeSpinner[i]
  • How to get Date from my JSpinner?
    this is the code of the Spinner Hini = new javax.swing.JSpinner(); Date date = new Date(); SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.MINUTE); JSpinner Hini = new JSpinner(sm); JSpinner.DateEditor de = new JSpinner.DateEditor(Hini, "hh:mm a"); de.getTextField().setEditable( true ); Hini.setEditor(de); And this is how i want it to get the values, but it always shows "00:00" `SimpleDateFormat formater = new SimpleDateFormat("HH/mm"); String spinnerValue = formater.format(Hini.getValue()); System.out.println(spinnerValue);` I also tried this but it always showed the
  • Swing 组件之间的新线(New Line Between Swing Components)
    问题 我已经尝试多次搜索如何多次格式化 java swing 元素,但我似乎找不到任何东西。 我曾尝试学习网格包布局,但它太复杂了。 有人可以告诉我如何使用 Swing 分隔摆动组件吗? 谢谢! 编辑 代码: import javax.swing.*; import java.awt.*; public class Format_Swing_Elemnts { public static void main(String[] args) { JLabel title = new JLabel("Bridge Settings:"); title.setFont(font); JLabel l1 = new JLabel("Number of Bars: "); JLabel l2 = new JLabel("Deck Elevation: "); JLabel l3 = new JLabel("Height of arch: "); JLabel l4 = new JLabel("Height of Pier: "); JLabel l5 = new JLabel("Cabel Anchorage: "); JLabel l6 = new JLabel("Concrete Type: "); final JLabel lM1 = new JLabel("meters")
  • Java JSpinner.DateEditor working with TimeZone March clock change
    I have a problem with the standard JSpinner.DateEditor (as probably does everyone else). When it works with the Java.util.Date class, it only gets a default format from the Locale set on the spinner. It appears to be TimeZone ignorant. When we have a Date that is on the March Clock change day which in the UK has no 2 o'clock - the time jumps from 01:59:59.999 AM to 03:00 AM with day light savings applied. Therefore in the JSpinner.DateEditor I do not want the user to see anything for 02:00 to 02:59:59.999. When the hour is on 1 and they click up, I want to jump to 3 and vice versa. Is it
  • jSpinner时间选择器模型编辑(jSpinner time picker model editing)
    问题 我有 jSpinner 用于时间选择。 问题 1:用户可以编辑时分和第二个分隔符“:”,并且可以写额外的数字,如“123”问题 2:模型总是获取当前时间。 我想要“00:00:00” 当我用代码写这个而不是“hh:mm:ss”时,用户不能编辑这些值。 我的代码是; spinner1 = new javax.swing.JSpinner(); SpinnerDateModel spinnermodel = new SpinnerDateModel(); spinnermodel.setCalendarField(Calendar.MINUTE); spinner1 .setModel(spinnermodel); spinner1 .setEditor(new JSpinner.DateEditor(spinner1 , "hh:mm:ss")); 任何帮助表示赞赏。 谢谢。 回答1 问题 1:用户可以编辑时分和第二个分隔符“:”,并且可以写额外的数字,如“123” 您可以使用编辑器的格式化程序通过 getTextField() 方法完成此操作: JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "HH:mm:ss"); DateFormatter formatter = (DateFormatter
  • JSpinner.DateEditor in Java Not Respecting TimeZone on Initialization
    I'm working on bugfix to an existing Swing Application that is using java dates and the Swing's JSpinner as a DateEditor. I'm trying to get the Editor to default to using UTC to display the time, rather than our local timezone. The application is running on Windows, using Java 8. The code I am using is below. import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; public class Test { public static void main(String [] args) { // Initialize some sample
  • New Line Between Swing Components
    I have tried searching multiple time on how to format java swing elements multiple times, but I can't seem to find anything. I have tried to learn the grid bag layout but it is all too complicated. Could someone please tell me how to space swing components with Swing? Thanks! EDIT Code: import javax.swing.*; import java.awt.*; public class Format_Swing_Elemnts { public static void main(String[] args) { JLabel title = new JLabel("Bridge Settings:"); title.setFont(font); JLabel l1 = new JLabel("Number of Bars: "); JLabel l2 = new JLabel("Deck Elevation: "); JLabel l3 = new JLabel("Height of arch
  • Setting Time Format for jspinner in swings
    I am working in a java swing application. In that application i have to take time input from user. I need to make a JSpinner for the time, only in the hh:mm am/pm format. i searched in properties but could not get this format. Please suggest me some way to display time in hh:mm am/pm format. I thank to all your valuable suggestions.
  • JSpinner 值更改事件(JSpinner Value change Events)
    问题 如何在 jSpinner 值更改时立即进行更新。 ChangeListener listener = new ChangeListener() { public void stateChanged(ChangeEvent e) { jLabel.setText(e.getSource()); } }; spinner1.addChangeListener(listener); 上面的代码不会自动更改标签文本,它需要您再次单击任何地方才能更新。 回答1 答案是配置 JFormattedTextField 中使用的格式化程序,它是微调器编辑器的子项: formatter.setCommitsOnValidEdit(true); 不幸的是,接触它就像介绍性句子一样冗长和肮脏: final JSpinner spinner = new JSpinner(); JComponent comp = spinner.getEditor(); JFormattedTextField field = (JFormattedTextField) comp.getComponent(0); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit
  • 如何将 JSpinner 的值设置为某个日期(How to set value of JSpinner to a certain date)
    问题 我有一个JSpinner ,我将它添加到JPanel我想将其时间设置为: GregorianCalendar calendar JSpinner spinner = new JSpinner(); spinner.setModel(model); pom.add(new JLabel("Date", JLabel.RIGHT)); pom.add(spinner); 当我收到 IllegalValue 异常时如何实现这一点。 回答1 一个应该做的: SpinnerDateModel model = new SpinnerDateModel(); model.setCalendarField(Calendar.DAY_OF_YEAR); JSpinner spinner = new JSpinner(); spinner.setModel(model); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); java.sql.Time time = new java.sql.Time(osoba.getDataZatrudnienia().getTime().getTime()); spinner.setValue(time);
  • JSpinner 专供时间使用(JSpinner exclusively for time)
    问题 我正在使用 JSpinner 输入时间; 但是,当我使用 getValue 方法时,我获得了 1970 年 1 月 1 日的时间,因为这是默认日期开始。 我怎样才能得到时间和独处的时间? 我对日期不感兴趣 注意:我已经使用了 dateEditor。 也许我的 JSpinnerDateModel 不合适? 回答1 您可以创建一个 JSpinner 实例并将其格式化为时间,然后在最后提取时间: JSpinner jSpinner1 = new JSpinner(); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date(0)); Date earliestDate = calendar.getTime(); calendar.add(Calendar.MINUTE, 1439); // number of minutes in a day - 1 Date latestDate = calendar.getTime(); SpinnerDateModel model = new SpinnerDateModel(earliestDate, earliestDate, latestDate, Calendar.MINUTE); jSpinner1.setModel(model)