-1

I want to set the todays/actual date in datechooser field automatically. I've looked a couple of Stackoverflow topics about this and couldnt find a way to complete this. I dont want to output the date with system.out.println(); but I just want to save it to database (MSSQL). So for example let me show you a photo of what I mean and if you have any questions about code I can send it here, but since I dont know what code to put here that will help you I will leave it blank. THANK YOU IN ADVANCE!

enter image description here

public String getDateToString(Date d) {
    DateFormat da = new SimpleDateFormat("dd-MM-yyyy");
    return da.format(d);
}

private void tabelaSelectedIndexChange() {
    final ListSelectionModel rowSM = table.getSelectionModel();
    rowSM.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                return;
            }

            ListSelectionModel rowSM = (ListSelectionModel) e.getSource();
            int selectedIndex = rowSM.getAnchorSelectionIndex();
            if (selectedIndex > -1) {
                Feedback f = ftm.getFeedback(selectedIndex);
                //idField.setText(p.getId().toString());
                arsyejaArea.setText(f.getArsyeja());
                dateChooser.setDate(f.getData());
                rcmb.setSelectedItem(f.getRecetaID());
                recetaCMB.repaint();
            }
        }
    });
}

private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    try {
        int row = table.getSelectedRow();

        if (arsyejaArea.getText().trim().isEmpty() || recetaCMB.getSelectedItem() == null || dateChooser.getDate() == null) {
            JOptionPane.showMessageDialog(this, "Nuk i keni mbushur te gjitha hapesirat!");
        } else if (row == -1) {
            Feedback f = new Feedback();
            //p.setId(Integer.parseInt(idField.getText()));
            f.setArsyeja(arsyejaArea.getText());
            f.setData(dateChooser.getDate());
            f.setRecetaID((Receta) rcmb.getSelectedItem());
            fr.create(f);
        } else {
            Feedback f = ftm.getFeedback(row);
            //Id nuk e lejojm me ndryshu vetem emertimin ose fielda tjere qe mundeni me pas
            f.setArsyeja(arsyejaArea.getText());
            f.setData(dateChooser.getDate());
            f.setRecetaID((Receta) rcmb.getSelectedItem());
            fr.edit(f);
        }
        //E krijojm ni metode per me i clear fieldat mbasi ti shtojme
        clear();
        loadTable();
    } catch (CrudFormException ex) {
        JOptionPane.showMessageDialog(this, "E dhena ekziston!");
    }
}
Jay Dee
  • 127
  • 1
  • 2
  • 10
  • Your question is a bit confusing since first you say you want to display the current date and time in the UI but then you say you simply want to save it to database. What's the problem here? Getting the date, printing it to the UI or saving it to the DB? If it's getting the date, calling the method `LocalDateTime.now()` will return an object `LocalDateTime` in the format "yyyy-MM-dd+T+HH:mm:ss.ms" which you might want to format using a `DateTimeFormatter`. – yur Jun 29 '20 at 10:32
  • @yur so the problem is that I cant get the date to show itself in the dateChooser, as you can see above (Data). The dateChooser field is empty, so I simply want to just display the actual PC time when somebody opens this form. I hope this made it clear. – Jay Dee Jun 29 '20 at 13:19
  • What Component are you using for your "dateChooser"? A simple JTextField or something more sophisticated like [JCalendar](https://toedter.com/jcalendar/)? – yur Jun 29 '20 at 13:27
  • Also it would probably help if you edited your question to at least include a code snippet of all the code around that "dateChooser" component. – yur Jun 29 '20 at 13:29
  • 1
    *"if you have any questions about code"* Yes. What is your [mre]? This would tell me 20 things I could not be bothered asking. – Andrew Thompson Jun 29 '20 at 13:29
  • 1
    BTW - since then, I decided to find out how easy it was to [do in a `JSpinner`](https://i.stack.imgur.com/s73o6.png). (checks watch) Pretty easy.. – Andrew Thompson Jun 29 '20 at 13:37
  • @yur I am using JCalendar – Jay Dee Jun 29 '20 at 14:24
  • @AndrewThompson I updated the code, you can check it out now. I hope it is clear... – Jay Dee Jun 29 '20 at 14:40
  • *"I updated the code"* I suggested posting an MRE, not some uncompilable code snippets. *"you can check it out now."* I'll pass. – Andrew Thompson Jun 29 '20 at 14:45

1 Answers1

1

The JCalendar constructors include at least 3 variants which seem to allow setting the initial or 'default' date. The Java Docs of an API should be the first thing checked when there are questions in regard to achieving a goal.

enter image description here


Given I wrote this minimal reproducible example to make the image seen in a comment above, I'll also share how to do it with a JSpinner. If nothing else of use, it demonstrates how to get the Date of .. right now as mentioned in all 3 of the relevant JCalendar constructors.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;

public class DefaultDateInSpinner {

    private JComponent ui = null;
    Date nowDate = new Date(System.currentTimeMillis()); // right NOW!
    Date endDate = new Date(System.currentTimeMillis()+1000000);
    SpinnerDateModel dateModel = new SpinnerDateModel(
            nowDate, nowDate, endDate, Calendar.NARROW_FORMAT);

    DefaultDateInSpinner() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        
        JSpinner spinner = new JSpinner(dateModel);
        ui.add(spinner, BorderLayout.LINE_START);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            DefaultDateInSpinner o = new DefaultDateInSpinner();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

If unable to make it work in the original code, edit to add an MRE. Note also that this example which can be copy/pasted and run, is shorter than the uncompilable code snippets included in the question.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433