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());
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; }