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 BufferedReader keyboard = new
    BufferedReader (new InputStreamReader (System.in));
  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();
    entry = Integer.parseInt(keyboard.readLine().trim());

    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!
Any suggestions...please!!

Thanks in advance
c18drmAsked:
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.

CEHJCommented:
Use two SimpleDateFormats
0
CEHJCommented:
Or simply subtract 12 hours from tc.getHours()  ;-)
0
c18drmAuthor 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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

imladrisCommented:
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;

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

public TimeCheck(String entry) {
      hours = Integer.parseInt(entry.substring(0, 3));
      minutes = Integer.parseInt(entry.substring(2));      
}
0
c18drmAuthor 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
0
c18drmAuthor 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?
0
CEHJCommented:
>>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;
}
0
imladrisCommented:
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.
0
CEHJCommented:
>>
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
0
c18drmAuthor 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
0
CEHJCommented:
It means 'cut the entry String before the 3rd character, and save the first bit into the variable'
0
Giant2Commented:
See here for an explanation about Date and Time.
http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html

Bye, Giant.
0
c18drmAuthor 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
0
Giant2Commented:
...and use the String properties here:
http://docs.rinet.ru/WebJPP/ch10.htm

Hope these links could help you.
Giant.
0
Giant2Commented:
ooops. the before link was about J++.
See here for Java1.4
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
0
CEHJCommented:
If it's easier to use int, then use imladris' approach
0
imladrisCommented:
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);
0
CEHJCommented:
If your TimeCheck class is in 24hr, then this will give you 12hr:

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


import java.io.*;

public class TimeTest {
      static BufferedReader keyboard = new
                  BufferedReader(new InputStreamReader(System.in));
      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();
                  entry = Integer.parseInt(keyboard.readLine().trim());

                  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");
            }
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
c18drmAuthor 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");
    }

}
0
imladrisCommented:
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.
0
CEHJCommented:
It's working here ...
0
c18drmAuthor Commented:
CEHJ,
How'd you mean its working??!!!
U get it to do everything its supposed to?
Been away working hence the delayed reply
0
CEHJCommented:
Well i'll show you what i get - hang on going to input the time here (2235)...
0
CEHJCommented:
(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 ..)
0
c18drmAuthor 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.
Very quick reply service aswell.
How do the points on the site get rewarded? what do you guys need to get the credit for helping?
0
c18drmAuthor 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!
0
CEHJCommented:
>>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
0
CEHJCommented:
8-)
0
Giant2Commented:
Sorry for not help you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.