0

I want to represent time with my time class. I can't use get and set methods.Only I can use listed methods on the code.But it doesn't work. It returns 0:0:0.

 public int addHours(int hours) { if(hours>=0&&hours<=23) { return hours; } return 0; } public int addMinutes(int minutes) { if(minutes>=0&&minutes<=59) { return minutes; } return 0; } public int addSeconds(int seconds) { if(seconds>=0&&seconds<=59) { return seconds; } return 0; } public String showTime() { return hours+":"+minutes+":"+seconds; } } 
4
  • 5
    your add...-methods don't add anything. Why are they called "addSeconds" and "addMinutes"? You should either refactor the code to actually add the value or rename the methods. Commented Feb 27, 2016 at 14:35
  • You should save them in your global variables like this.hours=hours otherwise their values will not be saved Commented Feb 27, 2016 at 14:36
  • use the java 8 time api, seriously! (LocalTime is what you're looking for). if you depend on java 7 or newer, then use JodaTime Commented Feb 27, 2016 at 14:38
  • int hours; int minutes; int seconds; public Time() { } public Time(int hours) { this.hours=hours; } public Time(int hours,int minutes) { this.hours=hours; this.minutes=minutes; } public Time(int hours,int minutes,int seconds) { this.hours=hours; this.minutes=minutes; this.seconds=seconds; } //where is my mistake? Commented Feb 27, 2016 at 14:40

2 Answers 2

2

your code does nothing.

you need to do something like this:

public void addHours( int hours ){ this.hours += hours; // add hours this.hours %= 24; // roll over at 24 hours } public void addMinutes( int minutes ){ this.minutes += minutes; // add minutes addHours(this.minutes/60); // carry over to hours this.minutes %= 60; // roll over at 60 minutes } public void addSeconds( int seconds ){ this.seconds += seconds; // add seconds addMinutes(seconds/60); // carry over to minutes this.seconds %= 60; // roll over at 60 seconds } 

(it probably won't matter, but this is not thread safe at all)

but this is generally a bad idea. Java 8 has a beautiful time api, pre Java-8 there is the JodaTime library (which is actually the basis of the Java 8 time api). It seems what you want to do could benefit from LocalTime:

LocalTime t = LocalTime.of(13,50,27).addHours(1).addMinutes(1).addSeconds(1); System.out.println(t.toString()); // prints 14:51:28 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks your solution but i found solution myself,thanks very much
0

Use java.util.Calendar and java.text.SimpleDateFormat:

Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR, 0); cal.add(Calendar.HOUR, 5); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); System.out.println(sdf.format(cal.getTime())); 

2 Comments

I Can't use any other methods or packages.Only listed methods
@TimuçinÇiçek you should specify in your question that it is a homework and that you're forced to respect constraints.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.