课外天地 李树青学习天地Java程序语言课件 → 程序代码——时间类


  共有18186人关注过本帖树形打印复制链接

主题:程序代码——时间类

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1945 积分:26736 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——时间类  发帖心情 Post By:2006/2/26 21:34:16 [只看该作者]

1、基本的功能
时间类:
public class Time
{
        private int hour;
        private int minute;
        private int second;
        
        public void setHour(int h)
        {
                hour=h;
        }
        
        public int getHour()
        {
                return hour;
        }
        
        public void setMinute(int m)
        {
                minute=m;
        }
        
        public int getMinute()
        {
                return minute;
        }
        public void setSecond(int s)
        {
                second=s;
        }
        
        public int getSecond()
        {
                return second;
        }
        
        public String toString()
        {
                return String.valueOf(hour)+":"+String.valueOf(minute)+":"+String.valueOf(second);
                //return ""+hour+":"+minute+":"+second;
        }
}

应用程序:
public class exec
{
        public static void main(String args[])
        {
                Time t1=new Time();
                
                t1.setHour(1);
                t1.setMinute(2);
                t1.setSecond(3);
                
                System.out.println(t1);
        }
}

2、加入验证代码
时间类:
public class Time
{
        private int hour;
        private int minute;
        private int second;
        
        public void setHour(int h)
        {
                if(h>=0 && h<=23)
                        hour=h;
                else
                        hour=0;
                //hour=(h>=0 && h<=23)?h:0;
        }
        
        public int getHour()
        {
                return hour;
        }
        
        public void setMinute(int m)
        {
                if(m>=0 && m<=59)
                        minute=m;
                else
                        minute=0;
        }
        
        public int getMinute()
        {
                return minute;
        }
        public void setSecond(int s)
        {
                if(s>=0 && s<=59)
                        second=s;
                else
                        second=0;
        }
        
        public int getSecond()
        {
                return second;
        }
        
        public String toString()
        {
                return String.valueOf(hour)+":"+String.valueOf(minute)+":"+String.valueOf(second);
                //return ""+hour+":"+minute+":"+second;
        }
}

应用程序:
public class exec
{
        public static void main(String args[])
        {
                Time t1=new Time();
                
                t1.setHour(66);
                t1.setMinute(2);
                t1.setSecond(3);
                
                System.out.println(t1);
        }
}

3、加入构造函数
时间类:
public class Time
{
        private int hour;
        private int minute;
        private int second;
        
        public Time()
        {
                hour=0;
                minute=0;
                second=0;
        }
        
        public Time(int h,int m,int s)
        {
                setHour(h);
                setMinute(m);
                setSecond(s);
        }
        
        
        public void setHour(int h)
        {
                if(h>=0 && h<=23)
                        hour=h;
                else
                        hour=0;
                //hour=(h>=0 && h<=23)?h:0;
        }
        
        public int getHour()
        {
                return hour;
        }
        
        public void setMinute(int m)
        {
                if(m>=0 && m<=59)
                        minute=m;
                else
                        minute=0;
        }
        
        public int getMinute()
        {
                return minute;
        }
        public void setSecond(int s)
        {
                if(s>=0 && s<=59)
                        second=s;
                else
                        second=0;
        }
        
        public int getSecond()
        {
                return second;
        }
        
        public String toString()
        {
                return String.valueOf(hour)+":"+String.valueOf(minute)+":"+String.valueOf(second);
                //return ""+hour+":"+minute+":"+second;
        }
}

应用程序:
public class exec
{
        public static void main(String args[])
        {
                Time t1=new Time(66,2,3);
                
                System.out.println(t1);
        }
}

4、加入格式化显示的代码,使之自动补零以显示两位数字
时间类:
public class Time
{
        private int hour;
        private int minute;
        private int second;
        
        public Time()
        {
                hour=0;
                minute=0;
                second=0;
        }
        
        public Time(int h,int m,int s)
        {
                setHour(h);
                setMinute(m);
                setSecond(s);
        }
        
        
        public void setHour(int h)
        {
                if(h>=0 && h<=23)
                        hour=h;
                else
                        hour=0;
                //hour=(h>=0 && h<=23)?h:0;
        }
        
        public int getHour()
        {
                return hour;
        }
        
        public void setMinute(int m)
        {
                if(m>=0 && m<=59)
                        minute=m;
                else
                        minute=0;
        }
        
        public int getMinute()
        {
                return minute;
        }
        public void setSecond(int s)
        {
                if(s>=0 && s<=59)
                        second=s;
                else
                        second=0;
        }
        
        public int getSecond()
        {
                return second;
        }
        
        public String toString()
        {
                String str="";
                String sh=String.valueOf(hour);
                if(sh.length()==1)
                        str=str+"0";
                str=str+sh+":";
                        
                sh=String.valueOf(minute);
                if(sh.length()==1)
                        str=str+"0";
                str=str+sh+":";
                        
                sh=String.valueOf(second);
                if(sh.length()==1)
                        str=str+"0";
                str=str+sh;
                
                return str;
        }
}

应用程序:
public class exec
{
        public static void main(String args[])
        {
                Time t1=new Time(12,2,3);
                
                System.out.println(t1);
        }
}
输出为:
12:02:03

此时更为有效的时间类为(注意toString()方法):
public class Time
{
        private int hour;
        private int minute;
        private int second;
        
        public Time()
        {
                hour=0;
                minute=0;
                second=0;
        }
        
        public Time(int h,int m,int s)
        {
                setHour(h);
                setMinute(m);
                setSecond(s);
        }
        
        
        public void setHour(int h)
        {
                if(h>=0 && h<=23)
                        hour=h;
                else
                        hour=0;
                //hour=(h>=0 && h<=23)?h:0;
        }
        
        public int getHour()
        {
                return hour;
        }
        
        public void setMinute(int m)
        {
                if(m>=0 && m<=59)
                        minute=m;
                else
                        minute=0;
        }
        
        public int getMinute()
        {
                return minute;
        }
        public void setSecond(int s)
        {
                if(s>=0 && s<=59)
                        second=s;
                else
                        second=0;
        }
        
        public int getSecond()
        {
                return second;
        }
        
        public String toString()
        {
                java.text.DecimalFormat df=new java.text.DecimalFormat("00");
                return df.format(hour)+":"+df.format(minute)+":"+df.format(second);
                
        }
}

5、加入格式化显示的代码,使之显示上下午时间(注意toString()方法)
时间类:
public class Time
{
        private int hour;
        private int minute;
        private int second;
        
        public Time()
        {
                hour=0;
                minute=0;
                second=0;
        }
        
        public Time(int h,int m,int s)
        {
                setHour(h);
                setMinute(m);
                setSecond(s);
        }
        
        
        public void setHour(int h)
        {
                if(h>=0 && h<=23)
                        hour=h;
                else
                        hour=0;
                //hour=(h>=0 && h<=23)?h:0;
        }
        
        public int getHour()
        {
                return hour;
        }
        
        public void setMinute(int m)
        {
                if(m>=0 && m<=59)
                        minute=m;
                else
                        minute=0;
        }
        
        public int getMinute()
        {
                return minute;
        }
        public void setSecond(int s)
        {
                if(s>=0 && s<=59)
                        second=s;
                else
                        second=0;
        }
        
        public int getSecond()
        {
                return second;
        }
        
        public String toString()
        {
                java.text.DecimalFormat df=new java.text.DecimalFormat("00");
                return df.format(hour%12)+":"+df.format(minute)+":"+df.format(second)+(hour<12?"AM":"PM");
                
        }
}

应用程序:
public class exec
{
        public static void main(String args[])
        {
                Time t1=new Time(13,2,3);
                
                System.out.println(t1);
        }
}
输出为:
01:02:03PM

[此贴子已经被作者于2010-12-12 07:38:49编辑过]

 回到顶部