我正在编写一个 Java 程序,我需要将小时和分钟增加 1 并以 HH:MM 格式输出当前时间。如果小时是 23,它应该回到 0 或分钟是 59,分钟应该回到 0,小时应该回到下一个小时。这是我到目前为止所拥有的,但我并不完全确定它是正确的。我也不确定这是否是使时间出现在小时和分钟之间的正确方法。这是我的代码:
//TimeOfDay program
public class TimeOfDay
{
private int hour;
private int minute;
/**
* Constructor for objects of class TimeOfDay
*/
public TimeOfDay( int h, int m )
{
setHr( h );
setMin( m );
String time = "17:46";
String [] values = time.split(":");
}
public TimeOfDay()
{
}
/**
- set the hour using an input parameter h
- set the minute using an input parameter m
- add one hour (increase hour by 1)
- add one minute (increase minute by 1)
- output the current time in HH:MM format
(single digit hour or minute must have a leading 0, e.g. 04:09)
*/
public void setHr (int h )
{
if (0 <= h && h < 23 )
hour = h;
else
hour = 0;
}
public void setMin (int m)
{
if (0 <= m && m < 59)
minute = m;
else
minute = 0;
}
public void addHour()
{
hour++;
if ( hour > 23 )
hour = 0;
}
public void addMinute()
{
minute++;
if ( minute > 59 )
{
minute = 0;
addHour(); //increment hour
}
}
public int printCurrentTime()
{
if (hour < 10)
System.out.print( "0" + hour + ":");
if (minute < 10)
System.out.print( "0" + minute);
return time.split;
}
}