is there anyway to not use Integer.Tostring? can I use the java.util.Date(); for
ID: 3842370 • Letter: I
Question
is there anyway to not use Integer.Tostring? can I use the java.util.Date(); for the getTime? here is my code
public class Clock {
public int getHours() {
int hour = new java.util.Date().getHours()
return hour;
}
public int getMinutes() {
int minute = new java.util.Date().getMinutes();
return minute;
}
public String getTime() {
String date = "";
String hr = Integer.toString(getHours());
if (hr.length() == 1)
date = date + "0" + hr + ":";
else
date = date + hr + ":";
String min = Integer.toString(getMinutes());
if (min.length() == 1)
date = date + "0" + min;
else
date = date + min;
return date;
}
}
public class WorldClock extends Clock {
private int offset;
public WorldClock(int offset) {
this.offset = offset;
}
public int getHours() {
int hour = new java.util.Date().getHours();
hour = (hour + offset) % 24;
return hour;
}
public int getMinutes() {
int minute = new java.util.Date().getMinutes();
return minute;
}
}
public class TestWorldClock
public static void main(String[] args) {
WorldClock clock = new WorldClock(0);
System.out.println("Time=" + clock.getTime());
}
}
Explanation / Answer
No there is no direct method in java.util.Date() to get the time. what you have written in the code is exactly the correct logic to return the time.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.