Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it L'avatar di salcam
    Registrato dal
    Dec 2005
    Messaggi
    193

    JSpinner e le date

    Salve a tutti,

    codice:
    private JSpinner date;
    ...
    date = new JSpinner(new SpinnerDateModel());
    date.setEditor(new JSpinner.DateEditor(date, "dd-MM-yyyy"));
    ...
    String data = date.getValue().toString();
    attraverso questo codice riesco a far comparire un JSpinner per la selezione della data nella forma "dd-MM-yyyy". Il mio problema è quando utilizzo getValue(), che al posto di stamparmi la data nel formato dd-MM-yyyy mi stampa una data in forma strana riportando giorno, mese, anno, ora, ecc....

    Come posso fare per farmi tornare la data nel formato "dd-MM-yyyy"?

    Grazie in anticipo

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,301

    Moderazione

    Il linguaggio anche nel titolo, come da Regolamento.

    Questo l'ho corretto io.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    se n'è parlato diverse migliaia di volte: usa SimpleDateFormat

    Se cerchi sul forum stesso dovrebbero uscirne abbastanza da darti il mal di testa.

    http://forum.html.it/forum/search.ph...by=&sortorder=
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #4
    Utente di HTML.it L'avatar di salcam
    Registrato dal
    Dec 2005
    Messaggi
    193
    Scusa alka, hai ragione.

    Andrea1979: le ho letto tutti i migliaia di thread aperti su questo argomento, ma ovviamente non ho trovato nulla che faccia a caso mio.
    Infatti il problema non è nel settare l'Editore per la data, bensì nel farmi ritornare il valore del JSpinner. Ad esempio al posto di stampare 01/04/2006, mi stampa : "Sat Apr 01 18:00:53 CEST 2006".

    Comunque già avevo provate il SimpleDateFormat.

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    codice:
    import javax.swing.*;
    import java.text.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class JSpinnerDemo extends JFrame implements ActionListener {
      
      private JSpinner date;
      private JTextArea ta;
      private JButton getDate;
      SimpleDateFormat sdf;
      
      public void actionPerformed (ActionEvent ae) {
        Date d = (Date)date.getValue();
        String formatted = sdf.format(d);
        ta.append(formatted+"\n");
      }
      
      public JSpinnerDemo() {
        super("JSpinner Demo");
        this.setSize(400,300);
        this.getContentPane().setLayout(new BorderLayout());
        
        sdf = new SimpleDateFormat("dd/MM/yyyy");
        
        date = new JSpinner(new SpinnerDateModel());
        date.setEditor(new JSpinner.DateEditor(date, "dd-MM-yyyy"));
        JPanel north = new JPanel();
        north.setLayout(new GridLayout(1,2));
        
        ta = new JTextArea();
        getDate = new JButton("Data");
        getDate.addActionListener(this);
        north.add(date);
        north.add(getDate);
        this.getContentPane().add(north, BorderLayout.NORTH);
        this.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);    
      }
      
      public static void main (String[] args) {
        JSpinnerDemo dm = new JSpinnerDemo();
      }
    }
    Buon weekend
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  6. #6
    Utente di HTML.it L'avatar di salcam
    Registrato dal
    Dec 2005
    Messaggi
    193
    Grazie mille Andrea1979, ora ci sono riuscito. Mi mancava sdf.format(d). Grazie ancora e buon weekend anche a te.

    Ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.