If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

24-hour Calculator

Hello, im new to programming, and im learning how, but i wanted to try to make some sort of program that will tell you how long you are at a place, such as work or school.

I call it the "School Calculator"

So it works like this:

You enter when your school starts, lets say 4
You enter when your school ends, lets say 530
You enter weather this is AM or PM, lets say there both PM
It will then tell you:

"You school is 1 hour, 30 minutes long."

Now i coded this almost perfuctly, it will tell you this, but it will put everything as just a regular number, like "Your school is 130 hours long"

So I relised im going to need some sort of 24-hour clock, so that i can brake it into minutes.

Can anyone tell me how to:

Make it treat every number as "time" and once it reaches 60 minutes, it turns into 1 hour, after it will display the 60 minutes section (the hours) into a diffrent catagory, so it will say in the code "your school is" + hours + " and " + minutes + "long"

Re: 24-hour Calculator

Also, im working on a password generator, i got it to work but, it will do the numbers 1 by one, how do i hide this and keep the digets the same? like, if i remove the line that stops it from showing, it will just do somthing like GGGGGGGGGG, instead of randomizing it like Gh5fTn

I want it to hide
H
HK
HKY
HKYD
HKYDE
HKYDEC
HKYDECT
HKYDECTA
HKYDECTAI
HKYDECTAIZ part, but still show the
Password Generater came up with: hkydectAiz part, thank you in advance for anyone who can help with any of these 2 problems.

Re: 24-hour Calculator

Don't ask 2 totally unrelated questions in the same thread, please start a new thread for the second question and leave this thread for the first question or it will get incredibly confusing.

When posting code please use code tags.

When dealing with time you need to use the java.util.Date class to hold time values and the java.text.DateFormat class to convert Date objects to and from Strings. Or better still download Joda time and use that library instead.

Re: 24-hour Calculator

Don't ask 2 totally unrelated questions in the same thread, please start a new thread for the second question and leave this thread for the first question or it will get incredibly confusing.

When posting code please use code tags.

When dealing with time you need to use the java.util.Date class to hold time values and the java.text.DateFormat class to convert Date objects to and from Strings. Or better still download Joda time and use that library instead.

BTW Your program doesn't take any notice of the AM/PM tags.

Ok.
Im new to this site, so I dont know how to use "Code tags"

java.util.date, could you explain this more for me? Like give me an example?

And yes, I dont have any tags for AM/PM, because when I was going through this, I did not need it, becasue it doesnt need to be reconized other then typed out/desplayed to the user of the program.

Re: 24-hour Calculator

At the bottom of each of my posts, in blue text, it shows how to use code tags.

java.util.date, could you explain this more for me? Like give me an example?

It's not java.util.date, it is java.util.Date - Java is case sensitive.

Google for something like "Java using DateFormat".

And yes, I dont have any tags for AM/PM, because when I was going through this, I did not need it, becasue it doesnt need to be reconized other then typed out/desplayed to the user of the program.

AM & PM make a big difference to the calculation and if you ask a user to supply information then the user will expect the program to use that information in a sensible way. If they told the program they arrived at school at 8 AM and left at 9 PM they would rightly expect the program to say they have been at school for 13 hours and not 1 hour.

But don't worry, if you use DateFormat with the correct format string it will handle AM/PM for you.

Re: 24-hour Calculator

Originally Posted by keang

At the bottom of each of my posts, in blue text, it shows how to use code tags.

It's not java.util.date, it is java.util.Date - Java is case sensitive.

Google for something like "Java using DateFormat".
AM & PM make a big difference to the calculation and if you ask a user to supply information then the user will expect the program to use that information in a sensible way. If they told the program they arrived at school at 8 AM and left at 9 PM they would rightly expect the program to say they have been at school for 13 hours and not 1 hour.

But don't worry, if you use DateFormat with the correct format string it will handle AM/PM for you.

Re: 24-hour Calculator

K so it didint really work out too well... that time thing is 5 hours ahead of my time (its 2:59PM now, it would say its 6:59PM)

Although I did find an alternitive solution to my code, and sorta got it working, theres still 1 problem though when I was debugging it, how would I do my operation backwards for AM?
Like if they enterd PM to start (Because mabey you have a night school or your working a little longer for your shift), this is what happens when the user types PM first (AM first works fine)

School Calculator selected!

When does your school start: 5

AM or PM?
pm
When does your school end: 2
AM or PM?
am
Your school is: 9 hours long.
---------------------------------------
AM to PM is also a little buggy (for example, if you put 5 am - 10pm, it will say its 7 hours (answer is 17)) Any idea how to resolve this issue?
Here is my code:

Re: 24-hour Calculator

K so it didint really work out too well... that time thing is 5 hours ahead of my time (its 2:59PM now, it would say its 6:59PM)

That's because you aren't using the correct time zone. But it doesn't really matter as you are only interested in the difference between the times and not what the times are. Once you have parsed the input and created the Date objects you can call the getTime() methods to get the number of milliseconds. Subtract one from the other and you have your time difference in milliseconds.

Although I did find an alternitive solution to my code, and sorta got it working, theres still 1 problem though when I was debugging it, how would I do my operation backwards for AM?

AM to PM is also a little buggy

That's 2 problems, not 1.

I've told you how to do it, if you don't want to take my advice that's your choice. I've also asked you to post code in code tags which you have also ignored, but this now effects my ability and willingness to help you as unformatted code in hard to read. So why should I help further if you are just going to ignore all the advice/instructions I give you?

Re: 24-hour Calculator

Originally Posted by keang

That's because you aren't using the correct time zone. But it doesn't really matter as you are only interested in the difference between the times and not what the times are. Once you have parsed the input and created the Date objects you can call the getTime() methods to get the number of milliseconds. Subtract one from the other and you have your time difference in milliseconds.

That's 2 problems, not 1.

I've told you how to do it, if you don't want to take my advice that's your choice. I've also asked you to post code in code tags which you have also ignored, but this now effects my ability and willingness to help you as unformatted code in hard to read. So why should I help further if you are just going to ignore all the advice/instructions I give you?

You did not tell me how to put code tags... All you told me was it was at the bottom of your comment... It wasnt...

Yes I relise its 2, but basicly there both related to the same topic at hand...