java yymmdd_Java开发网 - 求助!! (求代码 对yyyymmdd日期进行比较)

Posted by:aska0865

Posted on:2004-02-14 12:16

i just did the dateException lab, it's not the one you want but that may help someone who want to learn how to check a correct mmddyyy format

public class DateException extends Exception{

public DateException (){

super("Invalid Date");

}

public DateException (String message){

super(message);

}

public DateException(int mon, int day){

super("Month"+String.valueOf(mon)+" has only has "+String.valueOf(day)+"days. Try again.");

}

// Java core package

import java.awt.*;

import java.awt.event.*;

//Java extension pckages

import javax.swing.*;

public class ExceptionCal extends JFrame implements ActionListener{

private   JTextField textField;

private   JButton button;

private String date="",d;

private int mon,day,year;

private int daysOfMon[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

private int daysOfMonLeap[]={0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//set up GUI

public ExceptionCal(){

super("Enter your birth day");

//get content pane and set its layout

Container container = getContentPane();

container.setLayout(new FlowLayout());

//set up the text field and the button

textField = new JTextField(10);

button = new JButton("Enter");

//register listener to button

container.add(textField);

container .add(button);

button.addActionListener(this);

setSize(320,240);

setVisible(true);

}

// process GUI events

public void actionPerformed(ActionEvent event){

//check events

if(event.getSource()==button){

//call getDate method

getDate();

}

}

//method getDate from the text field

private void getDate(){

//get the string from test field

d=textField.getText();

try{

// read string to mon,day and year

mon=Integer.parseInt(d.substring(0,2));

day=Integer.parseInt(d.substring(2, 4));

year= Integer.parseInt(d.substring(4,8));

// check the valid date

checkDate();

}

//process improperly length input

catch(StringIndexOutOfBoundsException outOfBound){

JOptionPane.showMessageDialog(null,"invalid length of date");

}

//process improperly type input

catch(NumberFormatException formatExceptionCal){

JOptionPane.showMessageDialog(null,"invalid data format");

}

//process improperly formatted input

catch(DateException dataException){

JOptionPane.showMessageDialog(this, dataException.toString());

}

}

//method checkDate throwing exceptions

//when invalid date format occurs

private void checkDate()throws DateException{

//check the date length

if(d.length()!=8)throw new DateException("invalid Date Format");

//check the month

if(mon<1||mon>12)throw new DateException("Month only between 1-12");

//check the leap years

if(year%4==0){

if(day<0||day>daysOfMonLeap[mon]) throw new DateException(mon,daysOfMonLeap[mon]);

}

//check the rest of years

else if(day<0||day>daysOfMon[mon]) throw new DateException(mon,daysOfMon[mon]);

//dispaly the valid date

JOptionPane.showMessageDialog(null,

"day of birth: "+day+"\n month of birth "+mon+"\n year of birth:"+year);

}

//main method

public static void main(String[] args) {

ExceptionCal test= new ExceptionCal();

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} // end main

}//end class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值