如何验证 java swt text中输入的时间格式是否正确
/*
* http://zhidao.baidu.com/question/231229478.html
*
* 如何验证 java swt text中输入的时间格式是否正确
*/
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class CheckInputTimeWithSWT
{
private Shell shell;
private Text inputTimeText;
private Text checkResutlText;
private Button checkButton;
//标准时间格式,如 :2011-01-01 13:01:59
//年接受输入4位,或只输入后两位,月,日,时,分,秒接受输入两位(不足两位高位补0),或者只输入一位
//当采用12小时制后面需要加上 am 或者 pm ,如 2011-01-01 1:01:59 pm,否则默认 24 小时制
static String STANDART_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
//简单的筛选,还有进一步处理
static String STANDART_TIME_FORMAT_REGEX =
"(\\d{4}|\\d{2})-" +
"(\\d|\\d{2})-" +
"(\\d|\\d{2})\\s" +
"(\\d|\\d{2}):" +
"(\\d|\\d{2}):" +
"(\\d|\\d{2})" +
"(\\sam|\\spm|\\sAM\\sPM|)"
;
public void open() {
final Display display = new Display();
shell = new Shell();
shell.setSize(380, 250);
shell.setText("Check Time");
final Group group = new Group(shell, SWT.NONE);
group.setText("");
group.setBounds(20, 20, 320, 160);
//时间输入便签及输入文本框
final Label inputTimeLabel = new Label(group, SWT.NONE);
inputTimeLabel.setBounds(20, 20, 100, 25);
inputTimeLabel.setText("Input Time:");
inputTimeText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
inputTimeText.setBounds(150, 20, 150, 25);
inputTimeText.setEditable(true);
//检查结果便签及显示文本框
final Label checkResultLabel = new Label(group, SWT.NONE);
checkResultLabel.setBounds(20, 65, 100, 25);
checkResultLabel.setText("Check Result:");
checkResutlText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
checkResutlText.setBounds(150, 65, 150, 25);
checkResutlText.setEditable(false);
//按钮
checkButton = new Button(group, SWT.NONE);
checkButton.addSelectionListener
(
new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
checkTime();
}
}
);
checkButton.setBounds(20, 105, 75, 25);
checkButton.setText("CHECK");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
//时间格式检查
private void checkTime() {
String time = inputTimeText.getText().trim();
int year=0,month=0,day=0,hour=0,minute=0,second=0;
boolean isLeapYear;
boolean is12Pattern = true;
boolean isMorning = true;
boolean isLegalTime = false;
if(time.matches(STANDART_TIME_FORMAT_REGEX))
{
isLegalTime = true;
//年
year = Integer.parseInt(time.substring(0,time.indexOf('-')));
//月
time = time.substring(time.indexOf('-')+1);
month = Integer.parseInt(time.substring(0,time.indexOf('-')));
//日
time = time.substring(time.indexOf('-')+1);
day = Integer.parseInt(time.substring(0,time.indexOf(' ')));
//时
time = time.substring(time.indexOf(' ')+1);
hour = Integer.parseInt(time.substring(0,time.indexOf(':')));
//分
time = time.substring(time.indexOf(':')+1);
minute = Integer.parseInt(time.substring(0,time.indexOf(':')));
time = time.substring(time.indexOf(':')+1);
//秒
//24小时制
if(-1 == time.indexOf(' '))
{
is12Pattern = false;
second = Integer.parseInt(time);
}
//12小时制
else
{
is12Pattern = true;
second = Integer.parseInt(time.substring(0,time.indexOf(' ')));
time = time.substring(time.indexOf(' ')+1);
if(time.endsWith("am"))
{
isMorning = true;
}
else
{
isMorning = false;
}
}
//是否闰年
if( (year%400==0) ||(year%100!=0 && year%4==0))
{
isLeapYear = true;
}
else
{
isLeapYear = false;
}
//判断月份
if(month < 1 || month > 12)
{
isLegalTime = false;
}
else
{
if(
( month == 2 && isLeapYear && day >=1 && day <= 29) ||
( month == 2 && !isLeapYear && day >=1 && day <= 28) ||
( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 && day >=1 && day <= 31) ||
( month == 4 || month == 6 || month == 9 || month == 11 && day >=1 && day <= 30)
)
{
if( (is12Pattern && hour >= 0 && hour <= 12 ) || (!is12Pattern && hour >= 0 && hour <= 23 ))
{
if(minute >= 0 && minute <= 59 && second >= 0 && second <=59 )
{
isLegalTime = true;
}
else
{
isLegalTime = false;
}
}
else
{
isLegalTime = false;
}
}
else
{
isLegalTime = false;
}
}
}
if(isLegalTime)
{
//将合法的输入时间标准化
String standardTime ="";
//标准年
if(year <= 10)
{
standardTime += "200" + String.valueOf(year);
}
else if(year <= 100)
{
standardTime += "20" + String.valueOf(year);
}
else
{
standardTime += String.valueOf(year);
}
standardTime += "-";
//标准月
standardTime += ( (month<=9) ? "0" + String.valueOf(month) : String.valueOf(month)) + "-";
//标准日
standardTime += ( (day<=9) ? "0" + String.valueOf(day) : String.valueOf(day) ) + " ";
//标准时
standardTime += ( (hour<=9) ? "0" + String.valueOf(hour) : String.valueOf(hour) ) + ":";
//标准分
standardTime += ( (minute<=9) ? "0" + String.valueOf(minute) : String.valueOf(minute) ) + ":";
//标准秒
standardTime += ( (second<=9) ? "0" + String.valueOf(second) : String.valueOf(second) ) ;
//标准 12 小时后缀
if(is12Pattern)
{
if(isMorning)
{
standardTime += " AM";
}
else
{
standardTime += " PM";
}
}
checkResutlText.setText(standardTime);
}
else
{
//提示标准的时间格式
checkResutlText.setText(STANDART_TIME_FORMAT);
}
}
public static void main(String[] args) {
CheckInputTimeWithSWT window = new CheckInputTimeWithSWT();
window.open();
}
}
/*
* legal:
* 11-3-2 1:8:9 am
* 11-3-2 1:8:9 pm
* 11-03-02 01:08:09 am
* 00-2-29 13:14:15
*
* illegal:
* 00-2-29 13:14:15 pm
* 00-2-30 13:14:15
* 00-4-31 13:14:15
* 00-4-30 11:59:60 am
*/