# I'm looking to convert from 24hour clock to 12hour using a set driver

I have a set driver.........

import java.io.*;

public class TimeTest {
static PrintWriter screen = new PrintWriter (System.out, true);

public static void main (String[] args)
{
try
{
TimeCheck tc;
int entry;

screen.print ("Time on 24hr timetable? "); screen.flush();

tc = new TimeCheck(entry);
if (tc.isValid())
screen.println ("Thats" + tc.getHours() + "." +
tc.getMinutes() + tc.timeOfDay());
else screen.println("Can't cope");
}
catch (Exception e)
{
screen.println ("Input Problem");
}
}
}

And need to create the main class to allow it to run.
I've been mucking about with it for ages but going more in circles than anything else!

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Use two SimpleDateFormats
Commented:
Or simply subtract 12 hours from tc.getHours()  ;-)
Author Commented:
But the problem is that the input comes in the form of a normal time eg 1400, 2300.
How can I get that change into hours/minutes.
I can't simply subtract 12 from the input
Commented:
I'm not quite clear on what TimeCheck is or does.
But "military time" (like 1400) could be split up into hours and minutes with division and mod:

// convert it to integer:

int mt=-1;
try
{   mt=Integer.parseInt(entry);
} catch(NumberFormatException e)
{   // input problem
}
int hours=mt/100;
int min=mt%100;

Commented:
Presumably you parse this input when constructing TimeCheck (unposted)?
Commented:
Commented:
... such as

public TimeCheck(String entry) {
hours = Integer.parseInt(entry.substring(0, 3));
minutes = Integer.parseInt(entry.substring(2));
}
Author Commented:
Not sure I understand the comment from imladris.

As for comment from CEHJ,
what will the     (entry.substring(0, 3)); do??

don't get that bit
Author Commented:
also, would i need to parse this from a string to integer in my TimeCheck constructor if it has been parse'd at that stage in the driver?
Commented:
>>what will the     (entry.substring(0, 3)); do??

Get the hours from the entry.

TimeCheck may as well have a method to return 12hr format

public int getHours12() {
return hours > 12? (hours - 12) : hours;
}
Commented:
mt=Integer.parseInt(entry);

This line converts the String of "1400" to a number 1400.
Once you have a number you can do arithmetic operations with it. So:

int hours=mt/100;

This will divide mt (which is the number 1400) by 100, and leaving no remainder. So hours will wind up being 14. You could now, if needed, check if it is greater than 12, and then subtract 12, if that is desirable.

int min=mt%100;

This will get the remainder after dividing mt by 100. In this case min would wind up being 0. If mt were 1410, the min would wind up being 10.
Commented:
>>
also, would i need to parse this from a string to integer in my TimeCheck constructor if it has been parse'd at that stage in the driver?
>>

No - i just did it that way for convenience
Author Commented:
>>what will the     (entry.substring(0, 3)); do??

Get the hours from the entry.

In that case what does the (0,3) stand for? don't see where they've come from.
Thanks for all the help so far by the way
Commented:
It means 'cut the entry String before the 3rd character, and save the first bit into the variable'
Commented:
See here for an explanation about Date and Time.
http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html

Bye, Giant.
Author Commented:
so for example if done in the some notation as my driver where the string is parsed to an integer in there its obviously not going to let me say substring(in my constructor) as there are no strings involved.

this is the problem i'm havin as i have no flexibility with the given driver
Commented:
...and use the String properties here:
http://docs.rinet.ru/WebJPP/ch10.htm

Giant.
Commented:
See here for Java1.4
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
Commented:
If it's easier to use int, then use imladris' approach
Commented:
Right, entry is an integer (converted from the input).

So you can manipulate it by breaking out hours and minutes (as I indicated). You could then recombine it and pass it in to TimeCheck.

tc=new TimeCheck(hours*100+min);
Commented:
If your TimeCheck class is in 24hr, then this will give you 12hr:

int hours = tc.getHours();
if (hours > 12) {
hours -= 12;
}
Commented:
Here for converting into number formats & other things
http://www.devdaily.com/Dir/Java/Articles_and_Tutorials/Strings/
Commented:
This may be all you need depending on how TimeCheck is implemented:

import java.io.*;

public class TimeTest {
static PrintWriter screen = new PrintWriter(System.out, true);

public static void main(String[] args) {
try {
TimeCheck tc;
int entry;

screen.print("Time on 24hr timetable? ");
screen.flush();

tc = new TimeCheck(entry);
if (tc.isValid()) {
int hours = tc.getHours();
int minutes = tc.getMinutes();
screen.println("Thats" + hours + "." + minutes + tc.timeOfDay());
if (hours > 12) {
int hours12 = hours - 12;
screen.println("Thats" + hours12 + "." + minutes + tc.timeOfDay() + " in 12-hour clock");
}
}
else {
screen.println("Can't cope");
}
}
catch (Exception e) {
screen.println("Input Problem");
}
}
}

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
ok here we go......here is a copy of my TimeCheck class that i've prepared and been fiddling about with for a while.
There may be bits in the wrong place etc.
And by now you'll probably be getting the idea that i'm not too good at this.......yet!!!!!

But hopefully I'll get there.
Anyway when i run my program it compiles fine, asks me to enter the time then...nothing.
Anyway here we go:

import java.io.*;
import java.text.DecimalFormat;

class TimeCheck {

private int hour;    //0-23
private int minute;  //0-59
private int time;

public TimeCheck() {
}

public TimeCheck(int time){
this.time=time;
}

public boolean isValid(){
if(
time == ((time>=0 && time<2400) ? time:0))
return true;
else return false;
}

public int getHours(){
hour = time/100;
if (hour>12){
hour = hour - 12;
}
return hour;
}

public int getMinutes(){
minute = time%100;
return minute;
}

public String timeOfDay(){
DecimalFormat twoDigits = new DecimalFormat("00");

return((getHours() ==12 || getHours() == 0) ? 12: getHours() %12)+
twoDigits.format( getMinutes())+  ":" +
(getHours() <12 ? "am" : "pm");
}

}
Commented:
Job 1 is to figure out where the program "went".

If you have a debugger this is relatively easy. If not, you will have to make do with old fashioned method of putting in print statements to find out how far it got.
Commented:
It's working here ...
Author Commented:
CEHJ,
How'd you mean its working??!!!
U get it to do everything its supposed to?
Been away working hence the delayed reply
Commented:
Well i'll show you what i get - hang on going to input the time here (2235)...
Commented:
(Only tweaked on the println):

C:\java\dumpit>java TimeTest
Time on 24hr timetable? 2235
That's 10.35 1035:am

(ok the 'am's wrong but ..)
Author Commented:
oh you dancer!
so i was at least on sort-of the right lines!!
must be the way my JDE's set-up, i'll try it on the Linux machines we use at uni.
Look, much appreciation to you guys who've helped.
How do the points on the site get rewarded? what do you guys need to get the credit for helping?
Author Commented:
ps, what do u mean by only tweaked on the println? nothing major i'm guessing.

i try to run my program and I can half-see the result coming ok but the window very quickly shuts down. using JBuilder and it is set up using packages, not quite got to grips with the ins and outs of it yet!
Commented:
>>ps, what do u mean by only tweaked on the println? nothing major i'm guessing.

No - only a space and an apostrophe changed here and there ;-

>>using JBuilder and it is set up using packages, not quite got to grips with the ins and outs of it yet!

You must also get familiar with running at the command line too.

>>How do the points on the site get rewarded?

By you ;-) Just accept an answer. You can split points if you want
Commented:
8-)
Commented: