格式化输入日期时间控件
可以用jieformatteddatebox box = new jieformatteddatebox("yyyy年mm月dd日hh点mm分ss秒");输入
也可以用jieformatteddatebox box = new jieformatteddatebox("hh:mm:ss");来输入时间
还可以这样用jspinner spinner = new jspinner();jieformatteddatebox fdb = new jieformatteddatebox(format);spinner.setmodel(fdb);spinner.seteditor(fdb);
/////////////////////////////////////////////////package org.jie.ui.formattedbox;
import java.sql.timestamp;import java.text.simpledateformat;
import java.awt.event.focusevent;import java.awt.event.focuslistener;import javax.swing.jformattedtextfield;import javax.swing.spinnermodel;import javax.swing.swingconstants;import javax.swing.swingutilities;import javax.swing.event.changelistener;import javax.swing.event.documentevent;import javax.swing.event.documentlistener;import javax.swing.text.defaultformatterfactory;import javax.swing.text.maskformatter;
public class jieformatteddatebox extends jformattedtextfield implements spinnermodel{
private string format; private simpledateformat timeformat; private timestamp time;
public void settime(timestamp time){ if(time != null) setvalue(time); } public timestamp gettime(){ return time; }
public static string replace(string src,string replacesrc,string replacewith){ if(src == null) return null; stringbuffer sb = new stringbuffer(); int start = 0; int end = 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)); return sb.tostring(); }
public jieformatteddatebox(string format){ super(); this.format = format; timeformat = new simpledateformat(format); time = new timestamp(system.currenttimemillis());
try{ string mask = format; for(int i=0;i<alais.length;i++){ mask = replace(mask,alais[i],formats[i]); } maskformatter mf = new maskformatter(mask); mf.setplaceholdercharacter(_); setformatterfactory(new defaultformatterfactory(mf)); }catch(exception e){ e.printstacktrace(); }
settext(timeformat.format(time)); this.getdocument().adddocumentlistener(new documentlistener(){ public void insertupdate(documentevent e){ checktime(); } public void removeupdate(documentevent e){ checktime(); } public void changedupdate(documentevent e){} }); this.sethorizontalalignment(swingconstants.right); this.addfocuslistener(new focuslistener(){ public void focusgained(focusevent e){} public void focuslost(focusevent e){ setvalue(time); } }); }
void checktime(){ try{ string text = gettext(); if(text == null || text.equals("")) return; time = new timestamp(timeformat.parse(text).gettime()); }catch(exception ex){ try{ swingutilities.invokelater(new runnable() { public void run() { setvalue(time); } }); }catch(exception e){} }
}
public object getvalue(){ return this.gettext(); } public void setvalue(object value){ time = (timestamp)value; int index = this.getcaretposition(); if(time == null){ this.settext(""); }else this.settext(timeformat.format(time)); this.setcaretposition(index);//keep caret position } public object getnextvalue(){ return new timestamp(time.gettime()+getincrease(this.getcaretposition(),format,time)); } public object getpreviousvalue(){ return new timestamp(time.gettime()-getdecrease(this.getcaretposition(),format,time)); } public void addchangelistener(changelistener l){} public void removechangelistener(changelistener l){}
static string[] alais = {"yyyy","mm","dd","hh","hh","mm","ss"}; static string[] formats = {"####","##","##","##","##","##","##"}; static long[] times = {0,0,86400000,3600000,3600000,60000,1000}; public static final simpledateformat fulltimeformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
public static long getdecrease(int caret,string format,timestamp time){ if(testalias(format,caret,0)){ try{ string timestr = (integer.parseint(new simpledateformat("yyyy").format(time)) – 1) + "-" + new simpledateformat("mm-dd hh:mm:ss").format(time); return time.gettime() – fulltimeformat.parse(timestr).gettime(); }catch(exception e){ return 1000; } } else if(testalias(format,caret,1)){ try{ simpledateformat ym = new simpledateformat("yyyy-mm"); long thismonth = ym.parse(ym.format(time)).gettime(); long lastmonth = ym.parse(ym.format(new timestamp(thismonth-2*times[2]))).gettime();
int days = (int)((thismonth-lastmonth)/times[2]); int thisdays = integer.parseint(new simpledateformat("dd").format(time)); return (thismonth-lastmonth)+(((thisdays > days)?(thisdays-days):0)*times[2]); }catch(exception e){ return 1000; } } for(int i=2;i<alais.length;i++){ if(testalias(format,caret,i)){ return times[i]; } } return 1000; }
public static long getincrease(int caret,string format,timestamp time){ if(testalias(format,caret,0)){ try{ string timestr = (integer.parseint(new simpledateformat("yyyy").format(time)) + 1) + "-" + new simpledateformat("mm-dd hh:mm:ss").format(time); return fulltimeformat.parse(timestr).gettime() – time.gettime(); }catch(exception e){ return 1000; } } else if(testalias(format,caret,1)){ try{ simpledateformat ym = new simpledateformat("yyyy-mm"); long thismonth = ym.parse(ym.format(time)).gettime(); long nextmonth = ym.parse(ym.format(new timestamp(thismonth+31*times[2]))).gettime(); long nextnextmonth = ym.parse(ym.format(new timestamp(nextmonth+31*times[2]))).gettime();
int days = (int)((nextnextmonth-nextmonth)/times[2]); int thisdays = integer.parseint(new simpledateformat("dd").format(time)); return (nextmonth-thismonth)-(((thisdays > days)?(thisdays-days):0)*times[2]); }catch(exception e){ return 1000; } } for(int i=2;i<alais.length;i++){ if(testalias(format,caret,i)){ return times[i]; } } return 1000; }
private static boolean testalias(string format,int caret,int index){ for(int i=format.indexof(alais[index]);i>=0;i=format.indexof(alais[index],i+1)){ if(caret >= i && caret <= i+alais[index].length()) return true; } return false; }
}
