【问题标题】:Java command line arguments using hh:mm:ss format使用 hh:mm:ss 格式的 Java 命令行参数
【发布时间】:2015-07-04 09:01:49
【问题描述】:

我正在尝试制作一个简单的程序,允许用户使用 hh:mm:ss 格式的命令行参数输入 startTime 和 stopTime 并打印时钟运行的持续时间。我查看了时间部分的 API,但找不到那种格式。我读到您可以通过构造函数传递参数,但这不起作用。代码如下:

import java.time.LocalTime;
import java.time.Duration;




public class Clock {
    private LocalTime startTime;
    private LocalTime stopTime;
    private Duration duration;
    private int hours, minutes, seconds;


    // no argument constructor that initializes the startTime to the current time
    public Clock() {
        startTime = LocalTime.now();
    }
    public Clock(LocalTime start, LocalTime stop) {
        startTime = start;
        stopTime = stop;
    }


    //public Clock(start, stop) {

    //}

    // resets the startTime to the given time
    public void start(int h, int m, int s) {
        hours = ((h >= 0 && h < 24) ? h : 0);
        minutes = ((m >= 0 && m < 60) ? m : 0);
        seconds = ((s >= 0 && s < 60) ? s : 0);
        startTime = LocalTime.of(hours, minutes, seconds);
    }

    //a stop() method that sets the endTime to the given time
    public void stop(int h, int m, int s) {
        hours = ((h >= 0 && h < 24) ? h : 0);
        minutes = ((m >= 0 && m < 60) ? m : 0);
        seconds = ((s >= 0 && s < 60) ? s : 0);
        stopTime = LocalTime.of(hours, minutes, seconds);
    }

    //a getElapsedTime() method that returns the elapsed time in seconds
    public Duration getElapsedTime() {
        System.out.println("Difference is " + Duration.between(stopTime, startTime).
            toNanos()/1_000_000_000.0 + " Seconds.");
        duration = Duration.between(stopTime, startTime);
        return duration;
    }

}

这里是main 方法:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;


public class TestClock {

    public static void main(String[] args) {

       LocalTime argOne;
       LocalTime argTwo;
       argOne = LocalTime.parse(args[0]);
       argTwo = LocalTime.parse(args[1]);
       Clock clockOne = new Clock(argOne, argTwo);
       System.out.println(clockOne.getElapsedTime());
    }

}

【问题讨论】:

  • 但这也不起作用..您期望什么,发生了什么?错误是什么..
  • 你提到了命令行参数,然后你没有显示 main 方法,这是你获得命令行参数的唯一地方。您应该展示它 - 以及它访问的所有代码以获取您展示的方法。
  • @Codebender - 纠正我所说的,我的意思是我试图将参数直接传递给构造函数(我读到可以这样做)但它给出了一个不兼容的类型错误
  • @RealSkeptic - 现已添加
  • 那么,您必须在传递参数之前将它们解析为LocalTime 对象。无论您在哪里阅读,您都不必阅读 - 它们是错误的,或者您没有正确理解。

标签: java time format command-line-arguments


【解决方案1】:

“那么,您必须在传递参数之前将参数解析为 LocalTime 对象。” -RealSkeptic

这让我得到了我需要的东西,谢谢。

public class TestClock {

    public static void main(String[] args) {

       LocalTime argOne;
       LocalTime argTwo;
       argOne = LocalTime.parse(args[0]);
       argTwo = LocalTime.parse(args[1]);
       Clock clockOne = new Clock(argOne, argTwo);
       clockOne.getElapsedTime();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多