Java时间实现
发布时间:2020-12-15 02:03:35 所属栏目:Java 来源:网络整理
导读: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 marius.costa@yahoo.com */pub
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 <marius.costa@yahoo.com> */ 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); } } } 解决方法
您可以考虑添加方法:
等于() 哈希() 并实现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 ); Reminder reminder = Reminder.remindMe("Go for the milk",atSix ); 小时改变了(当然是错误的) atSix.setHour( 1 ); 对于“提醒”对象而言,变量何时是私有的将不会有任何用处,因为它的引用保留在外部并且无法控制它,因此它变得不可靠. 这可能是你可能会介绍的一个非常奇怪的错误.使用不可变对象不容易出错.这就是为什么Java中的核心对象如String,Integer和很多其他对象是不可变的. 如果你能读懂这本书:Effective Java它将在Java视角下转180度. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |