Which of these names is better: time.getHours() or time.hours()? And why?public class Time implements Serializable {    private final int hours;    private final int minutes;    public static Time from(Calendar calendar) {        int hoursIn24HourFormat = calendar.get(Calendar.HOUR_OF_DAY);        int minutes = calendar.get(Calendar.MINUTE);        return new Time(hoursIn24HourFormat, minutes);    }    public Time(int hours, int minutes) {        this.hours = hours;        this.minutes = minutes;    }    // or may be getHours() name is better?    public int hours() {        return hours;    }    // or may be getMinutes() name is better?    public int minutes() {        return minutes;    }    @Override    public boolean equals(Object obj) {        if (obj == null) {            return false;        }        if (obj == this) {            return true;        }        if (!(obj instanceof Time)) {            return false;        }        Time other = (Time) obj;        return (hours == other.hours) &&               (minutes == other.minutes);    }    @Override    public int hashCode() {        return toMinutesOfDay();    }    private int toMinutesOfDay() {        return hours * 60 + minutes;    }    @Override    public String toString() {        return twoDigitString(hours) + : + twoDigitString(minutes);    }    private static String twoDigitString(int timeComponent) {        return (timeComponent < 10)                ? (0 + timeComponent)                : String.valueOf(timeComponent);    }    public boolean before(Time time) {        return toMinutesOfDay() < time.toMinutesOfDay();    }    public boolean after(Time time) {        return toMinutesOfDay() > time.toMinutesOfDay();    }    public boolean within(Time fromIncluded, Time toIncluded) {        return (!fromIncluded.after(this)) && (!this.after(toIncluded));    }}
Proper naming for a Time class
java;datetime
I'd name the class TimeOfDay to be absolutely clear about its purpose.Consider adding validation to the constructor (0  hour  23 and 0  minutes  59).Because before() and after() are predicates, I'd rename them to isBefore(TimeOfDay t) and isAfter(TimeOfDay t).I prefer getHours() over hours().If you prefer hours(), you might consider just exposing public final int hours, minutes; instead.  Normally, you want to reserve the flexibility to change the internal representation of your object (for example, to store just minutesOfDay instead of hours and minutes).  However, since those fields are final, and you're more or less committed to the representation due to serialization, there's not much to be gained by wrapping the value in a method, other than consistency with tradition, and you would already violate tradition slightly anyway by choosing hours() over getHours().