Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-27
34
Medium Priority
?
256 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:c18drm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 9
  • 5
  • +2
34 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12422536
Use two SimpleDateFormats
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12422568
Or simply subtract 12 hours from tc.getHours()  ;-)
0
 

Author Comment

by:c18drm
ID: 12422629
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Assisted Solution

by:imladris
imladris earned 800 total points
ID: 12422703
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12422720
Presumably you parse this input when constructing TimeCheck (unposted)?
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12422742
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12422747
... such as

public TimeCheck(String entry) {
      hours = Integer.parseInt(entry.substring(0, 3));
      minutes = Integer.parseInt(entry.substring(2));      
}
0
 

Author Comment

by:c18drm
ID: 12422842
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
 

Author Comment

by:c18drm
ID: 12422877
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12422894
>>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
 
LVL 16

Expert Comment

by:imladris
ID: 12422901
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12422915
>>
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
 

Author Comment

by:c18drm
ID: 12423102
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12423140
It means 'cut the entry String before the 3rd character, and save the first bit into the variable'
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12423277
See here for an explanation about Date and Time.
http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html

Bye, Giant.
0
 

Author Comment

by:c18drm
ID: 12423296
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12423312
...and use the String properties here:
http://docs.rinet.ru/WebJPP/ch10.htm

Hope these links could help you.
Giant.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12423338
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12423339
If it's easier to use int, then use imladris' approach
0
 
LVL 16

Expert Comment

by:imladris
ID: 12423368
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12423371
If your TimeCheck class is in 24hr, then this will give you 12hr:

int hours = tc.getHours();
if (hours > 12) {
      hours -= 12;
}
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12423381
Here for converting into number formats & other things
http://www.devdaily.com/Dir/Java/Articles_and_Tutorials/Strings/
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 12423494
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
 

Author Comment

by:c18drm
ID: 12423921
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
 
LVL 16

Expert Comment

by:imladris
ID: 12424156
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12424399
It's working here ...
0
 

Author Comment

by:c18drm
ID: 12427491
CEHJ,
How'd you mean its working??!!!
U get it to do everything its supposed to?
Been away working hence the delayed reply
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12427554
Well i'll show you what i get - hang on going to input the time here (2235)...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12427575
(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
 

Author Comment

by:c18drm
ID: 12427650
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
 

Author Comment

by:c18drm
ID: 12427670
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12427748
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12432327
8-)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12432878
Sorry for not help you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question