Time类:
package demo;
import java.util.Calendar;
public class Time {
private int hour;
private int min;
private int sec;
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getSec() {
return sec;
}
public void setSec(int sec) {
this.sec = sec;
}
Time(){
Calendar cal = Calendar.getInstance();
this.hour = cal.get(Calendar.HOUR);
this.min = cal.get(Calendar.MINUTE);
this.sec = cal.get(Calendar.SECOND);
}
Time(long l){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(l);
this.hour = cal.get(Calendar.HOUR);
this.min = cal.get(Calendar.MINUTE);
this.sec = cal.get(Calendar.SECOND);
}
}
测试类:
package demo;
public class Test {
public static void main(String[] args) {
Time time1 = new Time();
Time time2 = new Time(555550000);
System.out.println(time1.getHour()+"小时\t"+time1.getMin()+"分钟\t"+time1.getSec()+"秒");
System.out.println(time2.getHour()+"小时\t"+time2.getMin()+"分钟\t"+time2.getSec()+"秒");
}
}
import java.util.Date; public class Timer { private int hour; private int minute; private int second; public int getHour () { return hour; } public Timer() { long curTime = System.currentTimeMillis(); this.setTime(curTime); } public Timer(long oldTime) { this.setTime(oldTime); } private void setTime (long time) { this.hour = new Date(time).getHours(); this.minute = new Date(time).getMinutes(); this.second = new Date(time).getSeconds(); } public void setHour (int hour) { this.hour = hour; } public int getMinute () { return minute; } public void setMinute (int minute) { this.minute = minute; } public int getSecond () { return second; } public void setSecond (int second) { this.second = second; } public static void main (String[] args) { Timer aTime = new Timer(); System.out.println("当前时间 " + aTime.getHour() + ":" + aTime.getMinute() + ":" + aTime.getSecond()); Timer bTime = new Timer(555550000); System.out.println("当前时间 " + bTime.getHour() + ":" + bTime.getMinute() + ":" + bTime.getSecond()); } }
import java.util.Date;
public class Time {
private int hours; private int minute; private int second;
public Time() { Date date = new Date(); hours = date.getHours(); minute = this.getMinute(); second = this.getSecond(); }
public Time(long ms) { Date date = new Date(ms); hours = date.getHours(); minute = this.getMinute(); second = this.getSecond(); }
public int getHour() { return hours; }
public void setHour(int hours) { this.hours = hours; }
public int getMinute() { return minute; }
public void setMinute(int minute) { this.minute = minute; }
public int getSecond() { return second; }
public void setSecond(int second) { this.second = second; }
public static void main(String[] args) { Time aTime = new Time(); System.out.println("当前时间 " + aTime.getHour() + ":" + aTime.getMinute() + ":" + aTime.getSecond()); Time bTime = new Time(555550000); System.out.println("当前时间 " + bTime.getHour() + ":" + bTime.getMinute() + ":" + bTime.getSecond()); }}