|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。控件
格局化输出日期工夫控件
能够用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;
publicclassJieFormattedDateBoxextendsJFormattedTextFieldimplementsSpinnerModel{
privateStringformat;privateSimpleDateFormattimeFormat;privateTimestamptime;
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();}
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){}}
}
publicObjectgetValue(){returnthis.getText();}publicvoidsetValue(Objectvalue){time=(Timestamp)value;intindex=this.getCaretPosition();if(time==null){this.setText("");}elsethis.setText(timeFormat.format(time));this.setCaretPosition(index);//keepCaretPosition}publicObjectgetNextValue(){returnnewTimestamp(time.getTime()+getIncrease(this.getCaretPosition(),format,time));}publicObjectgetPreviousValue(){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");
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;}
}
最后我再次声明,我并没有说不看好java,实际上我对java很乐观的,毕竟她正在不断改进中,我相信她总有一天会和.net并驾齐驱的 |
|