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

格式化输入日期时间控件

可以用
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;
}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值