程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 格式化輸入日期時間控件 JieFormattedDateBox

格式化輸入日期時間控件 JieFormattedDateBox

編輯:關於JAVA

格式化輸入日期時間控件

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

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

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

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

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved