Hello, I am new to java with some experience in C++.

I am trying to write code that reads two different times and displays the difference in hours and minutes.
0 is midnight and the input should be in 24hr format.
This program should work even if the first time input is later in the day than the second time input.

for example:
Time 1: 1255
Time 2: 1305
0 hours and 10 minutes.

also How would you restrict the time input from 0-23 for the first two characters and from 0-59 with the next two characters?

Thanks

You need to split the input in two parts - the first two digits and the last two. The first two digits become your our part and the last two digits become you minute part. Once you have done this, run your validations on both the hour parts for them to be in the range of 0-23 and in the same way run your validation on both the minute parts for them to be in the range of 0-59.

For calculating the difference in time you can do this :
Find out the larger of the two time quantities, this can be done by comparing the hour part and if hours are equal then comparing the mimute part. Once you have the larger of the two time quantities, there are three possibilities - 1. the minute part of the larger time quantity is greater than the minute part of the smaller time quantity, 2. the minute part of the larger time quantity is equal to the minute part of the smaller time quantity and 3. the minute part of the larger time quantity is lesser than the minute part of the smaller time quantity.
In the first case the time difference is calculated this way : subtract the smaller time quantity's hour part from the larger time quantity's hour part and then do the same for the minute part. The answer is your difference. In the second case, since the minute part is equal just subtract the hour part of the smaller time quantity from the hour part of the larger time quantity. Again the answer is your time diff. If you are able to see this, then you might be able to figure out that during programming you can club the first two cases together. The third case - this is the one you talk about in your example - is to be treated a little specially. Think of it this way, since the minute part of the larger time quantity is lesser than the minute part of the smaller time quantity, the last hour has turned but a full sixty minutes have not been passed from the minute in the minute part of the smaller time quantity (I hope you are able to visualize this, cause I am finding trouble to explain it to you in words) So the last turn of the hour shouldn't be counted in the hour difference, so when you subtract the smaller time quantity's hour part from the larger time quantity's hour part subtract one more from the larger time quantity's hour part and add these sixty minutes to the larger time quantity's minute part. Having done this, subtract the smaller time quantity's minute part from the larger time quantity's minute part which should have certainly be greater now (after the addition of the sixty minutes). You have the answer now. Remember that you have to subtract one more from the larger time quantities hour part then the smaller time quantities hour part - so in your example above you reach an hour difference of zero.
This leaves just one case, that is when while finding the larger of the two time quantities, you find that the hour part of both the time quantities match. Well in this case just find out the larger minute part and subtract the smaller one from it which should give you the difference. This still leaves one case which is when the minutes part match too, in that case you know the answer. Pheww !!! I need to change the keyboard now, too many keystrokes ;)

Hi Shimbardy ..
a quick search in google resulted in the following topics , i hope you visit them for useful :

and

and

and

commented: roseindia, are you kidding me???? One awful place they love to show-off most terrible solution and stolen code example. (Yes stolen, they do not give credit to author) -3

@eng.hosam84 :
I suggest you read the problem domain as specified by the OP, cause I am sure you haven't done that. The reason I say this is because, the first two of the four so called helpful links you have mentioned are completely irrelevant to the OP's question. They are comapring datetime difference whereas the OP is asking for the time difference in the format she has mentioned above. I have gone through both of the links and they do nothing to help the OP's cause.

To : verruckt24
I read the thread carefully . and i opened Netbeans to start working in a simple code to solve the case , but when i searched in google i found examples more valuable than what i was going to do , and these examples also catch more cases than 'Shimbardy' asked for.

and thank you for advising me .

eng.hosam

The examples you have sited certainly DO NOT catch the cases shimbardy talks about, thats for anybody to tell. Go have a closer look again if you find this surprising rather than covering up your act.

EDIT :

``````import java.util.*;

/** DateDiff -- compute the difference between two dates.
*/
public class DateDiff {
public static void main(String[] av) {
/** The date at the end of the last century */
Date d1 = new GregorianCalendar(2000,11,31,23,59).getTime();

/** Today's date */
Date today = new Date();

// Get msec from each, and subtract.
long diff = today.getTime() - d1.getTime();

System.out.println("The 21st century (up to " + today +
") is " + (diff / (1000*60*60*24)) + " days old.");
}
}``````

This is the code from one of the site examples you mention, do you think this solves the OP's problem. C'mon now I guess you know what you are saying.

@hosam

I agree with verruckt24, the first two links definitely do not touch the case being considered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.