⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time.java

📁 老师给的在线航班系统
💻 JAVA
字号:
package com.toa.abs.model;

import com.toa.abs.exceptions.IllegalTimeFormatException;

/**
 * 代表一个24小时进制的时间值,只包含小时和分钟值。
 * @author tangliang
 *
 */
public class Time {
	private int hour;
	private int minute;
	
	public Time(int hour,int minute){
		setHour(hour);
		setMinute(minute);
	}
	public static Time valueOf(String str)throws IllegalTimeFormatException{
		if(str.indexOf(":")==-1 || str.indexOf(":")!=str.lastIndexOf(":")){
			throw new IllegalTimeFormatException("无效的时间格式:"+str);
		}
		String h=str.split(":")[0].trim();
		String m=str.split(":")[1].trim();
		
		
		try {
			int hour=Integer.parseInt(h);
			int min=Integer.parseInt(m);
			
			Time t=new Time(hour,min);
			return t;
		} catch (NumberFormatException e) {
			throw new IllegalTimeFormatException("无效的时间格式:"+str);
		}
		
		
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		if(hour<0 || hour>23){
			throw new IllegalTimeFormatException("无效的时间值:Hour:"+hour);
		}
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		if(minute<0 || minute>59){
			throw new IllegalTimeFormatException("无效的时间值:Min:"+minute);
		}
		this.minute = minute;
	}
	
	public boolean equals(Object obj){
		if(obj instanceof Time){
			Time t=(Time)obj;
			if(t.getHour()==this.getHour() && t.getMinute()==this.getMinute()){
				return true;
			}
		}
		return false;
	}
	
	public int hashCode(){
		return (hour<<hour)^0x372a8bc9^(minute<<hour);
	}
	
	public String toString(){
		return hour+":"+minute;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -