java - Handling date format in JSpinner with SpinDateModel -


i have following lines of code:

    spinnerdatemodel spinmod=new spinnerdatemodel();     jspinner spin=new jspinner(spinmod);     //spin.seteditor(new jspinner.dateeditor(spin,"dd.mm.yyyy")); 

when display spinner can edit , write in month field two-digit number want.

if click on jspinner side can increase , decrease month or day numbers values legal. but, if add last outcommented line, wouldn't work, either.

the question how make jspinner accept legal dates custom format.

also, if make actionlistener added button gets date, "correct date" applying getvalue() directly jspinner, wrong 1 (the 1 entered) if apply date editor's format value obtained jspinner. want action listener correct value in custom format , want jspinner accept input in custom format. possible?

my test code below:

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; import java.text.*; //for date format  public class testdates{      jlabel jl=new jlabel("date:");     jframe jf=new jframe("test spinnerdateformat");      jbutton jb=new jbutton("get date");     spinnerdatemodel spinmod=new spinnerdatemodel();     jspinner spin=new jspinner(spinmod);      class jbhandler implements actionlistener{         public void actionperformed(actionevent evt){             jspinner.dateeditor de=(jspinner.dateeditor)spin.geteditor();             string wrongdate=de.getformat().format(spin.getvalue());             date okdate=(date)spin.getvalue();             system.out.println(">>>\n"+wrongdate+"\n"+okdate);         }     }      jpanel pane=new jpanel();      public testdates(){          jf.setlocationrelativeto(null);         jf.setdefaultcloseoperation(jframe.exit_on_close);          pane.add(jl);          jb.addactionlistener(new jbhandler());         pane.add(jb);          spin.setvalue(new date());//this doesn't work custom format         spin.seteditor(new jspinner.dateeditor(spin,"dd.mm.yyyy"));         pane.add(spin);          jf.add(pane);         jf.pack();         jf.setvisible(true);     }      public static void main(string[] args){      swingutilities.invokelater(new runnable(){         public void run(){             testdates tdate=new testdates();         }         });     } } 

if click on jspinner side can increase , decrease month or day numbers values legal. but, if add last outcommented line, wouldn't work, either.

the problem little (but pretty common) mistake in date pattern @ line:

spin.seteditor(new jspinner.dateeditor(spin,"dd.mm.yyyy")); 

in pattern "mm" refers minutes not months, causing different outputs. correct pattern is:

spin.seteditor(new jspinner.dateeditor(spin,"dd.mm.yyyy")); 

the question how make jspinner accept legal dates custom format.

if want prevent users type invalid dates, need play editor's formatter , disallow invalid inputs. see following snippet:

spinnerdatemodel model = new spinnerdatemodel(); jspinner spinner = new jspinner(model);  jspinner.dateeditor editor = new jspinner.dateeditor(spinner, "dd.mm.yyyy"); dateformatter formatter = (dateformatter)editor.gettextfield().getformatter(); formatter.setallowsinvalid(false); // makes want formatter.setoverwritemode(true); 

custom date-time formats

see customizing formats trail.

see following table extracted date , time patterns section in simpledateformat javadoc:

enter image description here


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -