2

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
sarahjames
  • 21
  • 1
  • 3
  • Is this homework? Post what you've done so far so we can help. – Mob Oct 06 '11 at 12:07
  • try this [link](http://stackoverflow.com/questions/7631470/how-to-format-a-date-in-java/7631508#7631508). It has a similar question. – Naveen Babu Oct 06 '11 at 12:08

3 Answers3

0

You have to use Joda-Time API.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

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

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

}

Shaftoe2702
  • 135
  • 1
  • 6