开发者

simple java program that enables 2 Time-format input and count the time-difference

I'd like to know how to make a simple program where user can gave 2 input time(hh:mm) forma开发者_开发技巧t ---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.


You have to use Joda-Time API.


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()));
}

}


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); 



        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜