六。时间的相关操作
(1)计算两个日期之间的天数
String date1 = rel.getString("lendtime");//获得上次借书的时间
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");//定义时间格式
String date2 = ft.format(new Date());//获得当前还书的时间
long sub = 0;
Date date11 = ft.parse(date1);//将SimpleDateFormat对象转换成Date对象
Date date22 = ft.parse(date2);
sub = date22.getTime() - date11.getTime();// getTime()获得的是从1970-1-1到现在的毫秒数
sub = sub / 1000 / 60 / 60 / 24;// 得到相差的天数,一天等于24*60*60*1000毫秒
(2)判断日期格式是否符合“年-月-日”的格式以及日期是否合法
public static boolean isRight(String date) {
Pattern p = Pattern.compile(\\d{4}+[-]\\d{1,2}+[-]\\d{1,2});// 括号内的是正则表达式d{1,2}表示-后面可以为一位或两位字符,d{4}表示只能是四个字符,不能多也不能少,这里属于正则表达式的知识
Matcher m = p.matcher(date);// 判断是否与相应的正则表达式匹配
if (!m.matches()) {
return false;
} // 得到年月日
String[] array = date.split("-");//将字符串以”-”分成字符串数组
int year = Integer.valueOf(array[0]);
int month = Integer.valueOf(array[1]);
int day = Integer.valueOf(array[2]);
boolean leap = (year % 400 == 0) | (year % 100 != 0) & (year % 4 == 0);
if (month < 1 || month > 12) {
return false;
}
int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };
if (leap) {
monthLengths[2] = 29;
} else {
monthLengths[2] = 28;
}
int monthLength = monthLengths[month];
if (day < 1 || day > monthLength) {
return false;
}
return true;
}
}
另外判断日期输入是否符合yyyy-mm-dd格式的超长正则表达式
^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[1 3578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1 -9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468] [048]|[3579][26])00))-02-29)$ <!--EndFragment--> |
(3)Calendar的用法
Calendar now=Calendar.getInstance();//获得当前系统时间的一个实例对象
int yearnow=now.get(Calendar.YEAR);
int monthnow=now.get(Calendar.MONTH);
int daynow=now.get(Calendar.DAY_OF_MONTH);
System.out.println(yearnow+" "+(monthnow+1)+" "+daynow);
String date1 = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());//获得现在系统的时间,格式输出为”年-月-日”
System.out.println(date1);
//输出的结果是2009 11 24
2009-11-24
七。两个构造方法中的特殊用法
(1)添加控件,巧用全局变量
(2)注意全局变量i,可以根据i的值来判断当前的对象属于哪个构造方法产生,然后OK按钮实现相应的事件。i=0,点击OK后程序退出,i等于1时,点击OK弹出对话框
class Search extends JFrame implements ActionListener {
private JLabel la_1,la_2,label_dialog;
private JButton button;
private JPanel pa_1,pa_2;//调用第一个构造方法的第二个构造方法中调用这些变量了
private JDialog dialog;
private int i;
public Search(){
i=0;
this.setSize(200, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2,1));
pa_1=new JPanel();
this.add(pa_1);
la_1=new JLabel("la_1 in pa_1");
pa_1.add(la_1);
pa_2=new JPanel();
this.add(pa_2);
la_2=new JLabel("la_2 in pa_2");
pa_2.add(la_2);
button=new JButton("OK");
pa_2.add(button);
button.addActionListener(this);
dialog=new JDialog(this,"提示",true);//this表示dialog添加到当前对象中,提示指对话框的标题,true前端显示
label_dialog=new JLabel("",JLabel.CENTER);//后面的Center表示label上的字显示在label中央
dialog.add(label_dialog);
dialog.setSize(100, 100);
// dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);
}
public Search(JButton bt){
this();
i=1;
//this.setLayout(new FlowLayout());//如果在最下层添加其他的控件,没有这句话,还是按照第一个构造方法中的边布局管理的形式。有了这句话,就可以把后面添加的控件按照流布局管理的方式添加到窗体中
pa_1.remove(la_1);
pa_1.add(bt);//因为pa_1是全局变量可以这样调用。另外还可以移除la_1控件
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand()=="OK") {
if(this.i==0){
System.exit(0);
}
if(this.i==1){
label_dialog.setText("你点击的是按钮");
dialog.setLocation(this.getX()+50, this.getY()+100);
dialog.setVisible(true);
}
}
}
}
public class SearchDemo{
public static void main(String[] args){
Search s=new Search();
s.setVisible(true);
// JButton bt=new JButton("按钮");
// Search s=new Search(bt);
// s.setVisible(true);
}
}
九、写应用程序时注意符合查询语句的使用,这个非常常用也非常重要,写时要理清各表和表中键值之间的关系。在这个图书出纳管理系统中,管理员要查询图书的去向,分析得到的sql语句是:
select bookname,lendername,lendtime from book,lender,lend where (lender.lenderid,lendtime) in (select lend.lenderid,lendtime from lend where lend.bookid=00000000)and book.bookid=00000000; 下面这个是在用c#重写时写的sql语句 String sql = "select Lender.姓名,book.书名,Lend.借书时间 from book,Lend,Lender where book.图书账号=Lend.图书账号 and Lender.学生账号=Lend.学生账号 and Lend.图书账号='" + textBox1.Text.Trim()+"'"; <!--EndFragment--> |
十、一个Java链表程序,可以从中看出内部类的用法,是在做这个系统后的学习所获,值得看看。里面有链表元素的添加、删除和查找,使用了内部类后是整个程序很方便。内部类的优点是方便的调用外部类(现为内部类)的私有属性和方法
class Link{
class Node{
private String name;
private Node next;
public Node(String name){
this.name=name;
}
public void addNode(Node next){
if(this.next==null){
this.next=next;
}else{
this.next.addNode(next);
}
}
public void printNode(){
System.out.print(this.name+"---->");
if(this.next!=null){
this.next.printNode();
}
}
public boolean searchNode(String name){
if(this.name.equals(name)){
return true;
}else{
if(this.next!=null){
return this.next.searchNode(name);
}
return false;
}
}
public void deleteNode(Node pre,String name){
if(this.name.equals(name)){
pre.next=this.next;
}else{
this.next.deleteNode(this, name);
}
}
};
private Node root;
public void add(String name){
Node newNode=new Node(name);
if(this.root==null){
this.root=newNode;
}else{
if(this.root.next!=null){
this.root.next.addNode(newNode);
}else{
this.root.next=newNode;
}
}
}
public void print(){
if(this.root!=null){
this.root.printNode();
}
}
public boolean search(String name){
if(this.root==null){
return false;
}else{
if(this.root.name.equals(name)){
return true;
}else{
if(this.root.next!=null){
return this.root.next.searchNode(name);
}else{
return false;
}
}
}
}
public void delete(String name){
if(this.search(name)){
if(this.root.name.equals(name)){
if(this.root.next!=null){
this.root=this.root.next;
}else{
this.root=null;
}
}else{
if(this.root.next!=null){
this.root.next.deleteNode(root,name);
}
}
}
}
}
public class LinkDemo {
public static void main(String[] args){
Link link=new Link();
link.add("one");
link.add("two");
link.add("three");
link.add("four");
link.print();
System.out.println();
System.out.println(link.search("three"));
System.out.println(link.search("five"));
link.delete("two");
link.print();
}
}
在外部直接使用内部类的实例化对象,用外部类.内部类 内部类对象实例=外部类实例.new 内部类();
Class Outer{
Class Inner{
};
}
Outer out=new Outer();
Outer.Inner in=out.new Inner();
一个内部类如果使用static声明,它就成为了外部类,这时候创建该内部类的实例对象是
Outer.Inner in=new Outer.Inner();
内部类也可以在一个方法中。在方法体中定义的内部类可以直接访问外部类中的各个成员,但是如果访问方法中有参数时,则要使用关键字final声明。
class Outer{
private String name="hello world!!!";
public void fun(final int temp){//带参数,参数用final修饰
class Inner{
public void print(){
System.out.println("temp="+temp);
System.out.println("name="+name);
}
};
new Inner().print();//调用内部类Inner中的print方法
}
}
public class test2 {
public static void main(String[] args) {
Outer out=new Outer();
out.fun(20); }
}