您的位置:首页 > 其它

格式化输入日期时间控件 JieFormattedDateBox

2009-07-30 16:20 501 查看
格式化输入日期时间控件

可以用

JieFormattedDateBoxbox=newJieFormattedDateBox("yyyy年MM月dd日HH点mm分ss秒");

输入

也可以用

JieFormattedDateBoxbox=newJieFormattedDateBox("HH:mm:ss");

来输入时间 软件开发网

还可以这样用

JSPinnerspinner=newJSpinner();

JieFormattedDateBoxfdb=newJieFormattedDateBox(format);

spinner.setModel(fdb);

spinner.setEditor(fdb);

/////////////////////////////////////////////////

packageorg.jie.ui.formattedbox;

importJava.sql.Timestamp;

importjava.text.SimpleDateFormat;

importjava.awt.event.FocusEvent;

importjava.awt.event.FocusListener;

importjavax.Swing.JFormattedTextField;

importjavax.swing.SpinnerModel;

importjavax.swing.SwingConstants;

importjavax.swing.SwingUtilities;

importjavax.swing.event.ChangeListener;

importjavax.swing.event.DocumentEvent;

importjavax.swing.event.DocumentListener;

importjavax.swing.text.DefaultFormatterFactory;

importjavax.swing.text.MaskFormatter; http://www.mscto.com
publicclassJieFormattedDateBoxextendsJFormattedTextFieldimplementsSpinnerModel{

privateStringformat;

privateSimpleDateFormattimeFormat;

privateTimestamptime; http://www.mscto.com
publicvoidsetTime(Timestamptime){

if(time!=null)

setValue(time);

}

publicTimestampgetTime(){

returntime;

}

publicstaticStringreplace(Stringsrc,StringreplaceSrc,StringreplaceWith){

if(src==null)returnnull;

StringBuffersb=newStringBuffer();

intstart=0;

intend=src.indexOf(replaceSrc);

while(end>=0){

sb.append(src.substring(start,end));

start=end replaceSrc.length();

end=src.indexOf(replaceSrc,start);

sb.append(replaceWith);

}

sb.append(src.substring(start));

returnsb.toString();

} http://www.mscto.com
publicJieFormattedDateBox(Stringformat){

super();

this.format=format;

timeFormat=newSimpleDateFormat(format);

time=newTimestamp(System.currentTimeMillis());

try{

Stringmask=format;

for(inti=0;i<alais.length;i ){

mask=replace(mask,alais[i],formats[i]);

}

MaskFormattermf=newMaskFormatter(mask);

mf.setPlaceholderCharacter('_');

setFormatterFactory(newDefaultFormatterFactory(mf));

}catch(Exceptione){

e.printStackTrace();

}

setText(timeFormat.format(time));

this.getDocument().addDocumentListener(newDocumentListener(){

publicvoidinsertUpdate(DocumentEvente){

checkTime();

}

publicvoidremoveUpdate(DocumentEvente){

checkTime();

}

publicvoidchangedUpdate(DocumentEvente){}

});

this.setHorizontalAlignment(SwingConstants.RIGHT);

this.addFocusListener(newFocusListener(){

publicvoidfocusGained(FocusEvente){}

publicvoidfocusLost(FocusEvente){

setValue(time);

}

});

}

voidcheckTime(){

try{

Stringtext=getText();

if(text==null||text.equals(""))

return;

time=newTimestamp(timeFormat.parse(text).getTime());

}catch(Exceptionex){

try{

SwingUtilities.invokeLater(newRunnable(){

publicvoidrun(){

setValue(time);

}

});

}catch(Exceptione){}

}

}
http://www.mscto.com
publicObjectgetValue(){

returnthis.getText();

}

publicvoidsetValue(Objectvalue){

time=(Timestamp)value;

intindex=this.getCaretPosition();

if(time==null){

this.setText("");

}else

this.setText(timeFormat.format(time));

this.setCaretPosition(index);//keepCaretPosition

}

publicObjectgetNextValue(){

returnnewTimestamp(time.getTime() getIncrease(this.getCaretPosition(),format,time));

}

public ObjectgetPreviousValue(){

returnnewTimestamp(time.getTime()-getDecrease(this.getCaretPosition(),format,time));

}

publicvoidaddChangeListener(ChangeListenerl){}

publicvoidremoveChangeListener(ChangeListenerl){}

staticString[]alais={"yyyy","MM","dd","HH","hh","mm","ss"};

staticString[]formats={"####","##","##","##","##","##","##"};

staticlong[]times={0,0,86400000,3600000,3600000,60000,1000};

publicstaticfinalSimpleDateFormatfullTimeFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); http://www.mscto.com
publicstaticlonggetDecrease(intcaret,Stringformat,Timestamptime){

if(testAlias(format,caret,0)){

try{

StringtimeStr=(Integer.parseInt(newSimpleDateFormat("yyyy").format(time))-1)

"-" newSimpleDateFormat("MM-ddHH:mm:ss").format(time);

returntime.getTime()-fullTimeFormat.parse(timeStr).getTime();

}catch(Exceptione){

return1000;

}

}

elseif(testAlias(format,caret,1)){

try{

SimpleDateFormatym=newSimpleDateFormat("yyyy-MM");

longthisMonth=ym.parse(ym.format(time)).getTime();

longlastMonth=ym.parse(ym.format(newTimestamp(thisMonth-2*times[2]))).getTime();

intdays=(int)((thisMonth-lastMonth)/times[2]);

intthisDays=Integer.parseInt(newSimpleDateFormat("dd").format(time));

return(thisMonth-lastMonth) (((thisDays>days)?(thisDays-days):0)*times[2]);

}catch(Exceptione){

return1000;

}

}

for(inti=2;i<alais.length;i ){

if(testAlias(format,caret,i)){

returntimes[i];

}

}

return1000;

}

publicstaticlonggetIncrease(intcaret,Stringformat,Timestamptime){

if(testAlias(format,caret,0)){

try{

StringtimeStr=(Integer.parseInt(newSimpleDateFormat("yyyy").format(time)) 1)

"-" newSimpleDateFormat("MM-ddHH:mm:ss").format(time);

returnfullTimeFormat.parse(timeStr).getTime()-time.getTime();

}catch(Exceptione){

return1000;

}

}

elseif(testAlias(format,caret,1)){

try{

SimpleDateFormatym=newSimpleDateFormat("yyyy-MM");

longthisMonth=ym.parse(ym.format(time)).getTime();

longnextMonth=ym.parse(ym.format(newTimestamp(thisMonth 31*times[2]))).getTime();

longnextNextMonth=ym.parse(ym.format(newTimestamp(nextMonth 31*times[2]))).getTime();

intdays=(int)((nextNextMonth-nextMonth)/times[2]);

intthisDays=Integer.parseInt(newSimpleDateFormat("dd").format(time));

return(nextMonth-thisMonth)-(((thisDays>days)?(thisDays-days):0)*times[2]);

}catch(Exceptione){

return1000;

}

}

for(inti=2;i<alais.length;i ){

if(testAlias(format,caret,i)){

returntimes[i];

}

}

return1000;

} 软件开发网

privatestaticbooleantestAlias(Stringformat,intcaret,intindex){

for(inti=format.indexOf(alais[index]);i>=0;i=format.indexOf(alais[index],i 1)){

if(caret>=i&&caret<=i alais[index].length())

returntrue;

}

returnfalse;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: