格式化输入日期时间控件
可以用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; }
}