Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java时间实现 包装时间玩具; 导入java.util.regex.Pattern; ** *Dayer是一个不可变的对象,它存储一天中以 *小时,分钟和秒。未定义日期或年份。 * *@作者马吕斯·科斯塔 */ 公共类{`在此处输入代码` private int hour;//一天中的小时 private int minute;//小时分钟数 private int second;//秒 私有字符串时间;//时间作为字符串 私有静态最终字符串时间_LONG_FORMAT=“([01]?[0-9]| 2[0-3]):[0-5][0-9]:[0-5][0-9]”; 私有静态最终字符串时间_SHORT_FORMAT=“([01]?[0-9]| 2[0-3]):[0-5][0-9]”; /** *类私有构造函数,该构造函数使用内部生成器创建新对象。 * *@param timeBuilder-定义为内部类的新日间对象的生成器。 */ 私人日间(建筑商计时建筑商){ this.hour=timeBuilder.hour; this.minute=timeBuilder.minute; this.second=timeBuilder.second; this.time=timeBuilder.time; } 公共整数getHour(){ 返程时间; } 公共int getMinute(){ 返回分钟; } 公共int getSecond(){ 返回第二; } @凌驾 公共字符串toString(){ 返回时间; } /** *Builder是一个内部类,它基于int参数创建新的日间对象 *(小时、分钟、秒),或通过解析格式化为 *“HH:mm”或“HH:mm:ss”。 */ 公共静态类生成器{ 私人整小时=0; 私人整数分钟=0; 私有整数秒=0; 私有字符串时间; /** *构造函数,它从格式化为的字符串参数创建生成器 *“HH:mm”或“HH:mm:ss”。 *@param time-必须格式化为“HH:mm”或“HH:mm:ss”。 */ 公共生成器(字符串时间){ 这个时间=时间; } /** *从字符串{@link#time}创建一个Dayday对象。 *首先解析字符串{@code time}以验证时间 *24小时格式,带正则表达式。 *否则,将抛出运行时异常。 * * *@返回白天 *如果字符串格式不正确,@将引发IllegalArgumentException。 *如果无法从字符串时间提取int值,@将引发NumberFormatException。 */ 公共日间活动时间(){ String[]timeUnits=time.split(“:”); if(Pattern.compile(TIME\u SHORT\u FORMAT).matcher(TIME.matches()){ this.hour=Integer.parseInt(时间单位[0]); this.minute=Integer.parseInt(时间单位[1]); }else if(Pattern.compile(TIME\u LONG\u FORMAT).matcher(TIME.matches()){ this.hour=Integer.parseInt(时间单位[0]); this.minute=Integer.parseInt(时间单位[1]); this.second=Integer.parseInt(时间单位[2]); }否则{ 抛出新的IllegalArgumentException(“时间格式无效”+ “(预期格式:“HH:mm”或“HH:mm:ss”); } 返回新的白天(本); } } }_Java - Fatal编程技术网

Java时间实现 包装时间玩具; 导入java.util.regex.Pattern; ** *Dayer是一个不可变的对象,它存储一天中以 *小时,分钟和秒。未定义日期或年份。 * *@作者马吕斯·科斯塔 */ 公共类{`在此处输入代码` private int hour;//一天中的小时 private int minute;//小时分钟数 private int second;//秒 私有字符串时间;//时间作为字符串 私有静态最终字符串时间_LONG_FORMAT=“([01]?[0-9]| 2[0-3]):[0-5][0-9]:[0-5][0-9]”; 私有静态最终字符串时间_SHORT_FORMAT=“([01]?[0-9]| 2[0-3]):[0-5][0-9]”; /** *类私有构造函数,该构造函数使用内部生成器创建新对象。 * *@param timeBuilder-定义为内部类的新日间对象的生成器。 */ 私人日间(建筑商计时建筑商){ this.hour=timeBuilder.hour; this.minute=timeBuilder.minute; this.second=timeBuilder.second; this.time=timeBuilder.time; } 公共整数getHour(){ 返程时间; } 公共int getMinute(){ 返回分钟; } 公共int getSecond(){ 返回第二; } @凌驾 公共字符串toString(){ 返回时间; } /** *Builder是一个内部类,它基于int参数创建新的日间对象 *(小时、分钟、秒),或通过解析格式化为 *“HH:mm”或“HH:mm:ss”。 */ 公共静态类生成器{ 私人整小时=0; 私人整数分钟=0; 私有整数秒=0; 私有字符串时间; /** *构造函数,它从格式化为的字符串参数创建生成器 *“HH:mm”或“HH:mm:ss”。 *@param time-必须格式化为“HH:mm”或“HH:mm:ss”。 */ 公共生成器(字符串时间){ 这个时间=时间; } /** *从字符串{@link#time}创建一个Dayday对象。 *首先解析字符串{@code time}以验证时间 *24小时格式,带正则表达式。 *否则,将抛出运行时异常。 * * *@返回白天 *如果字符串格式不正确,@将引发IllegalArgumentException。 *如果无法从字符串时间提取int值,@将引发NumberFormatException。 */ 公共日间活动时间(){ String[]timeUnits=time.split(“:”); if(Pattern.compile(TIME\u SHORT\u FORMAT).matcher(TIME.matches()){ this.hour=Integer.parseInt(时间单位[0]); this.minute=Integer.parseInt(时间单位[1]); }else if(Pattern.compile(TIME\u LONG\u FORMAT).matcher(TIME.matches()){ this.hour=Integer.parseInt(时间单位[0]); this.minute=Integer.parseInt(时间单位[1]); this.second=Integer.parseInt(时间单位[2]); }否则{ 抛出新的IllegalArgumentException(“时间格式无效”+ “(预期格式:“HH:mm”或“HH:mm:ss”); } 返回新的白天(本); } } }

Java时间实现 包装时间玩具; 导入java.util.regex.Pattern; ** *Dayer是一个不可变的对象,它存储一天中以 *小时,分钟和秒。未定义日期或年份。 * *@作者马吕斯·科斯塔 */ 公共类{`在此处输入代码` private int hour;//一天中的小时 private int minute;//小时分钟数 private int second;//秒 私有字符串时间;//时间作为字符串 私有静态最终字符串时间_LONG_FORMAT=“([01]?[0-9]| 2[0-3]):[0-5][0-9]:[0-5][0-9]”; 私有静态最终字符串时间_SHORT_FORMAT=“([01]?[0-9]| 2[0-3]):[0-5][0-9]”; /** *类私有构造函数,该构造函数使用内部生成器创建新对象。 * *@param timeBuilder-定义为内部类的新日间对象的生成器。 */ 私人日间(建筑商计时建筑商){ this.hour=timeBuilder.hour; this.minute=timeBuilder.minute; this.second=timeBuilder.second; this.time=timeBuilder.time; } 公共整数getHour(){ 返程时间; } 公共int getMinute(){ 返回分钟; } 公共int getSecond(){ 返回第二; } @凌驾 公共字符串toString(){ 返回时间; } /** *Builder是一个内部类,它基于int参数创建新的日间对象 *(小时、分钟、秒),或通过解析格式化为 *“HH:mm”或“HH:mm:ss”。 */ 公共静态类生成器{ 私人整小时=0; 私人整数分钟=0; 私有整数秒=0; 私有字符串时间; /** *构造函数,它从格式化为的字符串参数创建生成器 *“HH:mm”或“HH:mm:ss”。 *@param time-必须格式化为“HH:mm”或“HH:mm:ss”。 */ 公共生成器(字符串时间){ 这个时间=时间; } /** *从字符串{@link#time}创建一个Dayday对象。 *首先解析字符串{@code time}以验证时间 *24小时格式,带正则表达式。 *否则,将抛出运行时异常。 * * *@返回白天 *如果字符串格式不正确,@将引发IllegalArgumentException。 *如果无法从字符串时间提取int值,@将引发NumberFormatException。 */ 公共日间活动时间(){ String[]timeUnits=time.split(“:”); if(Pattern.compile(TIME\u SHORT\u FORMAT).matcher(TIME.matches()){ this.hour=Integer.parseInt(时间单位[0]); this.minute=Integer.parseInt(时间单位[1]); }else if(Pattern.compile(TIME\u LONG\u FORMAT).matcher(TIME.matches()){ this.hour=Integer.parseInt(时间单位[0]); this.minute=Integer.parseInt(时间单位[1]); this.second=Integer.parseInt(时间单位[2]); }否则{ 抛出新的IllegalArgumentException(“时间格式无效”+ “(预期格式:“HH:mm”或“HH:mm:ss”); } 返回新的白天(本); } } },java,Java,不要这样做。抵制编写自己的日期/时间代码的所有诱惑。它会咬你的 使用标准日期类-即使它看起来是浪费 使用.< /p> 可以考虑添加方法: equals() hash() 并实现可比较的接口 int比较(对象其他) 此外,建议将其设置为不可变 例如,如果您使用此类检查是否必须发生某些事情: package timeToys; import java.util.regex.Pattern; ** * A DayTime is an immutable object that stores a

不要这样做。抵制编写自己的日期/时间代码的所有诱惑。它会咬你的


使用标准日期类-即使它看起来是浪费

使用.< /p> 可以考虑添加方法:

equals()

hash()

并实现可比较的接口

int比较(对象其他)

此外,建议将其设置为不可变

例如,如果您使用此类检查是否必须发生某些事情:

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);
        }
    }
}
如果您这样使用它:

 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"
     }
  }
时间变了(当然是错了)

当变量
是私有变量时,“提醒”对象将没有任何用处,因为它的引用被保留在外部,无法控制它,因此变得不可靠

你可能会引入一个非常奇怪的bug。使用不可变对象不太容易出错。这就是为什么Java中的核心对象,如String、Integer和其他许多对象是不可变的


如果你能读到这本书:它将使你的Java视角180度改变。

检查-一个强大而简单的Java.util、joda time等替代品。

我认为,“你觉得它怎么样”不是一个真正的问题。你能更具体地说你想知道什么吗?绝对同意。二传手不应该在那里。我还删除了接受int值的公共构造函数。
  DateTime atSix = new DateTime( 18, 0, 0 );

  Reminder reminder = Reminder.remindMe("Go for the milk", atSix );
  atSix.setHour( 1 );