请高手帮忙,J2SE里面有没有开源的控件可以用来选DATE或者DATETIME的? 去看看http://www.lavantech.com/index.shtml 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.lavantech.com/index.shtml这个是要钱的啊,还不便宜哦 http://www.zdnet.com.cn/developer/code/story/0,2000081534,39099895-3,00.htmDateChooser,很不错 http://www.zdnet.com.cn/developer/code/story/0,2000081534,39099895-3,00.htm那个还可以用,不过就是没有时间的选择啊 给个可以格式化输入日期时间的可以用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);BTW,一般来说时间是用来录入的而不是选择的/////////////////////////////////////////////////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; }} 【在线急等】熟悉Swt高手请进,有难度的问题 谈谈Java中提出接口(Interface)的好处和作用 急!这种输入怎么才能实现?(小弟新手) 做过的进:JAVA怎么取客户端的网卡号 技术总监都搞不懂的JDBC问题,向大家求救 使用Eclips 导入VE(Visual Editor)的问题(在线等) 我想手工运行一个程序,为什么不成功? 请问:在sql里面如何实现时间的大于等于的查询? java对数据库操作的时候是不是一定要在本地建立一个odbc数据源?? 求大神指导 请教String类中的replaceFirst方法 用udp来传文件可行吗?
这个是要钱的啊,还不便宜哦
那个还可以用,不过就是没有时间的选择啊
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);BTW,一般来说时间是用来录入的而不是选择的/////////////////////////////////////////////////
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;
}}