0

这是输出:

时间是上午 8:3:8 时间是 8:3:8

这是所需的输出:

时间是 08:03:08 AM 时间是 08:03:08

因此,我需要将其转换为所需的输出,如果是 8:03:04,则应该像上面那样显示 8 小时 3 分 4 秒。个位数前面有零

  package Assignment2;


  class clock{

  public static void main(String[] args) {
   clock timer = new clock();
   timer.setTime(8,02,59);
    timer.incTime(9);
     //timer.incTime(60);
       timer.display12hr();
     timer.display24hr();



           }


private int minutes, hours, seconds;

clock()
{
    minutes = 0;

    hours = 0;

    seconds = 0;

}
clock(int x, int y, int z)

{

    hours = x;

    minutes = y;

    seconds = z;
}

void setTime(int x, int y, int z)
{
    hours = x;
    minutes = y;
    seconds = z;
}

void incTime(int y)

{

    if (seconds + y > 59)

    {

        seconds = (seconds + y) - 60;
        minutes++;
    }
    else
        seconds += y;

}

void display12hr()

{
    if (hours > 12)
    {
        System.out.println("The time is " + (hours - 12) + ":" + minutes + ":" + seconds
                + "PM");
    }

    else
    {
        System.out.println("The time is " + (hours) + ":" + minutes + ":" + seconds +"AM");
    }
}

void display24hr()
{
    System.out.println("The time is " + (hours) + ":" + minutes + ":" + seconds);
}

}

4

4 回答 4

2

尝试使用格式化输出:

System.out.printf("%02d:%02d:%02d", hours, minutes, seconds);

当数字没有 2 位时,这将用 0 填充数字,如此所述。

于 2013-09-07T02:31:17.380 回答
0

你可以使用DecimalFormat类。

例子:

int[] numbers = { 1, 5, 10, 15};
for(int num : numbers) {
    DecimalFormat formatter = new DecimalFormat("00");
    System.out.println(formatter.format(num));
}

输出:

01
05
10
15
于 2013-09-07T02:36:28.967 回答
0

对于您要打印的任何数字,如果它 < 10,则必须在前面添加一个 0(假设所有值都打印到 2 个位置)。听起来像一个函数的工作......

于 2013-09-07T02:28:38.090 回答
0

使用 Calendar 和 SimpleDateFormat 对象分别为您提供了丰富的日期和时间功能以及丰富的日期和时间格式化功能。

您可以手动控制与您的班级类似的时间测量......

    String datePattern = "hh:mm:ss";
    SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
    Calendar cal = Calendar.getInstance(Locale.US);
    System.out.println("Time = " + formatter.format(cal.getTimeInMillis()));
    cal.clear();
    cal.set(2013, 8, 6, 0, 0, 0);
    System.out.println("Start Time = " + formatter.format(cal.getTimeInMillis()));
    cal.set(2013, 8, 6, 8, 2, 59);
    System.out.println("Start Time = " + formatter.format(cal.getTimeInMillis()));
    cal.add(Calendar.SECOND, 9);
    System.out.println("End Time 12 hours = " + formatter.format(cal.getTimeInMillis()));
    datePattern = "HH:mm:ss";
    formatter = new SimpleDateFormat(datePattern);
    cal.add(Calendar.HOUR, 8);
    System.out.println("End Time 24 hours = " + formatter.format(cal.getTimeInMillis()));

结果输出:

    Time = 12:32:31
    Start Time = 12:00:00
    Start Time = 08:02:59
    End Time 12 hours = 08:03:08
    End Time 24 hours = 16:03:08

或者,如果您只是想要与事件关联的开始和停止时间......

    datePattern = "yyyy-mm-dd hh:mm:ss:SS";
    formatter = new SimpleDateFormat(datePattern);
    System.out.println("start = " + formatter.format(Calendar.getInstance(Locale.US).getTimeInMillis()));
    System.out.println("end = " + formatter.format(Calendar.getInstance(Locale.US).getTimeInMillis()));

结果输出:

    start = 2013-32-07 12:32:31:656
    end = 2013-32-07 12:32:31:656

这取决于您的目标,但您有很多选择。

于 2013-09-07T04:43:36.813 回答