Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:04:47+00:00 2026-05-13T10:04:47+00:00

package timeToys; import java.util.regex.Pattern; ** * A DayTime is an immutable object that stores

  • 0
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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:04:48+00:00Added an answer on May 13, 2026 at 10:04 am

    You may consider add the methods:

    equals()

    hash()

    and implement the Comparable interface

    int compareTo( Object other )

    Also, it is recommendable to make it immutable.

    For instance if you use this class to check if something has to happen:

     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"
         }
      }
    

    If you use it like this:

      DateTime atSix = new DateTime( 18, 0, 0 );
    
      Reminder reminder = Reminder.remindMe("Go for the milk", atSix );
    

    And the hour is changed ( by mistake of course )

      atSix.setHour( 1 );
    

    It won’t be any use to the “Reminder” object that the variable when is private, because it’s reference is kept outside and doesn’t have the control over it, hence it become unreliable.

    That would be a very strange bug you may introduce. Using immutable objects is less error prone. That’s why core objects in Java like String, Integer, and lots others are immutable.

    If you can read this book: Effective Java it will turn 180 deg you Java perspective.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the service is going to be running at the… May 13, 2026 at 7:32 pm
  • Editorial Team
    Editorial Team added an answer You can find a list of collations here, along with… May 13, 2026 at 7:32 pm
  • Editorial Team
    Editorial Team added an answer A Plugin isn't the same as what you are thinking.… May 13, 2026 at 7:32 pm

Related Questions

package abc; class DependencyDataCollection { private int sNo; private String sessionID; private int noOfDependency;
Package Load Failure Package 'Microsoft.VisualStudio.Xaml' has failed tot load properly. . . yadda, yadda,
package samples.flexstore { import flash.events.Event; public class ProductThumbEvent extends Event { public static const
package gui; public class Solver { void solveIt(){ CubeGui.moveThat(); } } I am trying
package classes.events { import flash.events.Event; public class ASSEvent extends Event { public static const

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.