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. 爪哇 - 线程资源和执行时间
- 4. 爪哇While循环检查的时间,但不执行方法
- 5. 的webdriver - 爪哇 - 如何执行行动
- 6. 爪哇 - 每行
- 7. 爪哇 - 换行
- 8. 爪哇 - 并行
- 9. 爪哇断言禁用Map.remove的执行()
- 10. 爪哇 - 执行方法只有一次
- 11. 爪哇时钟推进时间
- 12. 爪哇 - 可运行
- 13. 爪哇 - 按时间排序阵列
- 14. 爪哇 - 显示时间与在JOptionPane的
- 15. 爪哇:计算持续时间
- 16. 逆阵列(爪哇)// Invertir阵列(爪哇)
- 17. 意外行为(爪哇)
- 18. 爪哇,用system.out.print行结束
- 19. 爪哇 - NoClassDefFoundError的运行
- 20. 爪哇定时器代码
- 21. 爪哇秋千定时器
- 22. 爪哇 - 抛出:IllegalArgumentException时.buildConfiguration()
- 23. 爪哇脚本时间与给定时间比较
- 24. GridBagLayout爪哇
- 25. 爪哇从
- 26. 爪哇 - 继承
- 27. 爪哇CryEngine 3
- 28. 爪哇:checkPositionRow
- 29. 爪哇 - NoClassDefFoundError的
- 30. 爪哇 - 对PMI
“你认为它”是不是一个真正的问题,我想。你能更具体地说明你想知道什么吗? – bmargulies 2010-01-08 22:50:50