package timeToys;
import java.util.regex.Pattern;
**
* A DayTime is an immutable object that stores a moment of day represented in
* hour, minutes and seconds. Day or year are not defined.
*
* @author marius.costa <[email protected]>
*/
public class DayTime {`enter code here`
private int hour;// hour of the day
private int minute;// minute of the hour
private int second;// second of the minute
private String time;// time as string
private static final String TIME_LONG_FORMAT = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
private static final String TIME_SHORT_FORMAT = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
/**
* Class private constructor that creates new objects using inner builder.
*
* @param timeBuilder - builder for new DayTime objects defined as inner class.
*/
private DayTime(Builder timeBuilder) {
this.hour = timeBuilder.hour;
this.minute = timeBuilder.minute;
this.second = timeBuilder.second;
this.time = timeBuilder. time;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
@Override
public String toString() {
return time;
}
/**
* Builder is a inner class that creates new DayTime objects based on int params
* (hour, minute, second), or by parsing a String param formated as
* 'HH:mm' or 'HH:mm:ss'.
*/
public static class Builder {
private int hour = 0;
private int minute = 0;
private int second = 0;
private String time;
/**
* Constructor that creates a Builder from a String param formated as
* 'HH:mm' or 'HH:mm:ss'.
* @param time - must be formated as 'HH:mm' or 'HH:mm:ss'.
*/
public Builder(String time) {
this.time = time;
}
/**
* Creates a DayTime object from the String {@link #time}.
* The String {@code time} is innitialy parsed to validate time
* in 24 hours format with regular expression.
* If not, RuntimeExceptions will be thrown.
*
*
* @return DayTime
* @throws IllegalArgumentException if the string isn't right formated.
* @throws NumberFormatException if int values cannot be extracted from String time.
*/
public DayTime createTime() {
String[] timeUnits = time.split(":");
if(Pattern.compile(TIME_SHORT_FORMAT).matcher(time).matches()) {
this.hour = Integer.parseInt(timeUnits[0]);
this.minute = Integer.parseInt(timeUnits[1]);
} else if(Pattern.compile(TIME_LONG_FORMAT).matcher(time).matches()) {
this.hour = Integer.parseInt(timeUnits[0]);
this.minute = Integer.parseInt(timeUnits[1]);
this.second = Integer.parseInt(timeUnits[2]);
} else {
throw new IllegalArgumentException("Invalid time format" +
" (Expected format: 'HH:mm' or 'HH:mm:ss').");
}
return new DayTime(this);
}
}
}
Q
爪哇時間執行
3
A
回答
1
您可以考慮添加方法:
equals()
hash()
,並實現Comparable接口
int compareTo(Object other)
而且,推薦,使其不可變的。
例如,如果你使用這個類來檢查,如果事情已經發生:
class Remainder {
private String what;
private DateTime when;
public static Remainder remindMe(String what, DateTime when) {
Reminder r = new Reminder();
r.what = what;
r.when = when;
}
public boolean isTimeAlready() {
//return DateTime.Builder.createTime().compareTo(this.when) > 0;
// implemented somehow
return isCurrentTimeGreaterThan(this.when); // assume it return true if current time is after "when"
}
}
如果你使用這樣的:
DateTime atSix = new DateTime(18, 0, 0);
Reminder reminder = Reminder.remindMe("Go for the milk", atSix);
和時針改變(按課程的錯誤)
atSix.setHour(1);
這不會是任何使用的「提醒」對象,變量when
是私人的,因爲它的引用被保留在外面,並且沒有對它的控制,因此它變得不可靠。
這將是一個非常奇怪的錯誤,你可能會介紹。使用不可變對象不太容易出錯。這就是爲什麼Java中的核心對象如String,Integer和其他許多其他類型是不可變的原因。
如果你可以讀這本書:Effective Java它將轉變180度你Java的觀點。
+0
絕對同意。安裝者不應該在那裏。我也刪除了採用int值的公共構造函數。 – marcos 2010-01-08 23:24:20
11
不這樣做。抵制所有誘惑寫你自己的日期/時間代碼。它會咬你
使用標準的日期類 - 甚至壽它似乎是一種浪費
12
使用JodaTime。
0
退房http://veyder-time.enblom.org - 一個功能強大且簡單的替代java.util中,喬達時間等
相關問題
- 1. 爪哇 - 並行
- 2. 爪哇 - 每行
- 3. 爪哇斷言禁用Map.remove的執行()
- 4. 爪哇 - 執行方法只有一次
- 5. 爪哇 - 顯示時間與在JOptionPane的
- 6. 爪哇:計算持續時間
- 7. 爪哇月度計時器
- 8. 爪哇鞦韆定時器
- 9. 硒,爪哇,waitForCondition
- 10. 爪哇thesauraus
- 11. 爪哇 - 在
- 12. 爪哇 - 由
- 13. 爪哇的StringIndexOutOfBoundsException
- 14. 爪哇:enum toString()
- 15. 爪哇排序
- 16. 爪哇優化
- 17. 爪哇從長
- 18. 爪哇檢查,如果在間隔
- 19. 爪哇重力bug
- 20. 爪哇 - 調用Arrays.copyOfRange
- 21. 爪哇VoIP記錄
- 22. 爪哇 - 雙比較
- 23. 爪哇 - ARGS []爲int
- 24. 爪哇由令牌
- 25. 爪哇 - 線程池
- 26. 爪哇 - 防暴API
- 27. 爪哇 - 使用GSON
- 28. 爪哇實施htonl
- 29. 爪哇oop - 繼承
- 30. 爪哇指數法
「你認爲它」是不是一個真正的問題,我想。你能更具體地說明你想知道什麼嗎? – bmargulies 2010-01-08 22:50:50