简单的java程序,支持2种时间格式输入并计算时差

发布于 2024-12-08 17:54:49 字数 414 浏览 1 评论 0原文

我想知道如何编写一个简单的程序 用户可以给出 2 个输入时间(hh:mm)格式 --- 并接收经过的时间作为输出。

该程序将运行并使用户能够写入时间输入。 也许是一个通过命令提示符运行的简单程序。

例如,当程序运行时:

  1. 请写/输入开始时间!
    (例如:用户将以“hh:mm”格式写入,例如;19.14)

  2. 写入/输入结束时间: (用户将以“hh:mm”格式写入,如 23.40)

  3. 输出将类似于:“您已过去 hh(小时)”

我一直在谷歌上搜索时间格式的事情, 甚至使用 simpledateformat, 但当我尝试将输入实现到类或其他任何内容时,我有点混淆了。

有人可以帮我解决这个问题吗?

提前致谢。

I'd like to know how to make a simple program
where user can gave 2 input time(hh:mm) format ---and receive the elapsing time as the output.

the program will run and enable user to write time-input..
maybe a simple program that run through command prompt.

Example, when the program runs:

  1. Please Write/input the starting time!
    (eg: user will write in "hh:mm" format like; 19.14)

  2. Write/input the end time:
    (user will write in "hh:mm" format like; 23.40)

  3. The output will be like: "You have elapsed hh (hour)"

I've been google-about the time format things,
or even used simpledateformat,
but I just kind of mixed up when trying to implement the input into the classes orwhatsoever.

is there anybody can help me to solve this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-12-15 17:54:49

您必须使用 Joda-Time API。

You have to use Joda-Time API.

厌味 2024-12-15 17:54:49

我认为您对 Date 构造函数的调用没有按照您的想法进行。

日期的三个参数构造函数是:

Date(intyear,intmonth,intdate)
已弃用。从 JDK 版本 1.1 开始,被 Calendar.set(year + 1900,month,date) 或 GregorianCalendar(year + 1900,month,date) 取代。

试试这个:

日期(int 年,int 月,int 日期,int 小时,int 分,int 秒)
已弃用。从 JDK 版本 1.1 开始,替换为 Calendar.set(year + 1900, Month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, Month, date, hrs, min, sec)

如果您只关心经过的时间,然后为两者插入相同的日期,然后减去。

试试这个:

Date d1 = new Date( 15, 10, 44);
System.out.println(d1.toString());

看看发布的日期。

  • 如果这是家庭作业,那么讲师可能会反对使用任何日期/时间 API 类方法。

导入 java.util.Date;

公共课 SomeOnesHomeWork {

public final int secondsInMinute = 60;
public final int millisecondsPerSecond =1000;
public final int milliPerMinute = secondsInMinute * millisecondsPerSecond;

public int convertMillisecondsToMinutes(long milliseconds){
     return (int) (milliseconds / milliPerMinute);
}

public int differenceInMinutes(long beginTime,long endTime){
    return convertMillisecondsToMinutes(endTime - beginTime);
}

public static void main(String[] argc){

    //Make the dates
    Date d1 = new Date(1999,12,31,23,45,44);
    Date d2 = new Date(1999,12,31,23,59,59);

    //We Could use Static methods, probably should
    SomeOnesHomeWork aHomeWorkObject = new SomeOnesHomeWork();
    System.out.println("The time between two dates in minutes: "
        + aHomeWorkObject.differenceInMinutes(
                d1.getTime(),d2.getTime()));
}

}

I don't think your call to the Date constructor is doing what you think it is.

The three arg constructor for date is:

Date(int year, int month, int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Try this one:

Date(int year, int month, int date, int hrs, int min, int sec)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec)

If all you care about is the time elapsed, then plugin the same date for both, then subtract.

try this:

Date d1 = new Date( 15, 10, 44);
System.out.println(d1.toString());

see what date is puts out.

  • If this is homework, an instructor will probably frown upon the use of any of the date/time API classes methods.

import java.util.Date;

public class SomeOnesHomeWork {

public final int secondsInMinute = 60;
public final int millisecondsPerSecond =1000;
public final int milliPerMinute = secondsInMinute * millisecondsPerSecond;

public int convertMillisecondsToMinutes(long milliseconds){
     return (int) (milliseconds / milliPerMinute);
}

public int differenceInMinutes(long beginTime,long endTime){
    return convertMillisecondsToMinutes(endTime - beginTime);
}

public static void main(String[] argc){

    //Make the dates
    Date d1 = new Date(1999,12,31,23,45,44);
    Date d2 = new Date(1999,12,31,23,59,59);

    //We Could use Static methods, probably should
    SomeOnesHomeWork aHomeWorkObject = new SomeOnesHomeWork();
    System.out.println("The time between two dates in minutes: "
        + aHomeWorkObject.differenceInMinutes(
                d1.getTime(),d2.getTime()));
}

}

网名女生简单气质 2024-12-15 17:54:49

我猜你正在寻找这个..

import java.text.*;

    import java.util.*;


    public class DifferenceBetweenTimes {

        public static void main(String[] args) throws InterruptedException{





            Date d1 = new Date( 15, 10, 44);
                   Date d2 = new Date( 15, 30, 44);



            SimpleDateFormat dfm = new SimpleDateFormat ("HH:mm:ss") ;



            System.out.println(dfm.format(d1));

            System.out.println(dfm.format(d2));



           long timeDiff = ( Math.abs( d1.getTime() - d2.getTime()) / 1000 ) / 60;

             System.out.println(timeDiff); 



        }

    }

I guess You are looking for this..

import java.text.*;

    import java.util.*;


    public class DifferenceBetweenTimes {

        public static void main(String[] args) throws InterruptedException{





            Date d1 = new Date( 15, 10, 44);
                   Date d2 = new Date( 15, 30, 44);



            SimpleDateFormat dfm = new SimpleDateFormat ("HH:mm:ss") ;



            System.out.println(dfm.format(d1));

            System.out.println(dfm.format(d2));



           long timeDiff = ( Math.abs( d1.getTime() - d2.getTime()) / 1000 ) / 60;

             System.out.println(timeDiff); 



        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文